Does a wrapper class calling a COM component through C# need to implement the Dispose pattern?

StackOverflow https://stackoverflow.com/questions/1559227

  •  21-09-2019
  •  | 
  •  

문제

I have a class written in c# which is acting as a wrapper around a COM component. The COM component is early bound and the RCW has been generated by Visual Studio. Should I implement a dispose pattern in my wrapper class to clean up the COM reference, or should I just let the GC handle it, as it already has a RCW?

도움이 되었습니까?

해결책

There is rarely a need to implement Dispose but there are often good reasons to do so.

If the COM object represents a significant resource that needs to be released quickly then that might be a good reason to implement Dispose.

In your dispose method you can do:-

 System.Runtime.InteropServices.Marshal.ReleaseComObject(myRCW);

Hence disposing your class will release the COM object immediately.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top