为什么COM对象不使用IDisposable? [英] Why don't COM object use IDisposable?
问题描述
为什么COM对象在其CLR可调用包装器中不使用IDisposable?
Why don't COM object use IDisposable in their CLR Callable Wrappers?
推荐答案
大概是这样的 IDisposable
实现将调用 Marshal.ReleaseComObject 。
Presumably such an IDisposable
implementation would call Marshal.ReleaseComObject.
在某些情况下,调用Marshal.ReleaseComObject是一个好主意,例如使Office应用程序在从.NET客户端自动化后退出。。
There are cases where calling Marshal.ReleaseComObject is a good idea, for example to get an Office application to quit after automation from a .NET client.
但是,正如 Marshal.ReleaseComObject
的文档所述,一般情况下不应使用它-可能根本不使用它。 -proc COM对象。 这是一篇博客文章,其中包含一些更详细的信息。
But as the documentation for Marshal.ReleaseComObject
states, it should not be used in the general case - and probably not at all for in-proc COM objects. Here's a blog post with some more detailed info.
因此,鼓励人们通过从 IDisposable
Hence it would not have been a good idea to encourage people to use it by calling it from an IDisposable
implementation in the RCW.
但是,有趣的是Silverlight 4 AutomationFactory.CreateObject
方法 返回的动态对象是 IDisposable
。测试似乎表明实际上确实发布了COM参考,尽管文档有点稀疏。
However, what's interesting is that the Silverlight 4 AutomationFactory.CreateObject
method does return a dynamic object that is IDisposable
. And tests seem to show that this does in fact release the COM reference, though documentation is a bit sparse.
这篇关于为什么COM对象不使用IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!