通过 C# 调用 COM 组件的包装类是否需要实现 Dispose 模式?
我有一个用 C# 编写的类,它充当 COM 组件的包装器。 COM 组件是早期绑定的,并且 RCW 已由 Visual Studio 生成。我应该在包装类中实现处置模式来清理 COM 引用,还是应该让 GC 处理它,因为它已经有一个 RCW?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很少需要实施 Dispose,但通常有充分的理由这样做。
如果 COM 对象代表需要快速释放的重要资源,那么这可能是实现 Dispose 的一个很好的理由。
在您的 dispose 方法中,您可以执行以下操作: -
因此,处置您的类将立即释放 COM 对象。
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:-
Hence disposing your class will release the COM object immediately.