我将在我的 C# 应用程序中使用 COM 互操作。
这是我的代码:
IMyInterface objData = MyCOMClass.GetData();
我的问题是:
我是否需要使用System.Runtime.InteropServices.Marshal.ReleaseComObject(objData);释放objData上的资源?
谢谢阅读。
我将在我的 C# 应用程序中使用 COM 互操作。
这是我的代码:
IMyInterface objData = MyCOMClass.GetData();
我的问题是:
我是否需要使用System.Runtime.InteropServices.Marshal.ReleaseComObject(objData);释放objData上的资源?
谢谢阅读。
是的,除非您可以等待垃圾回收清理它。
值得注意的是,COM互操作会为每个接口创建一个COM引用。
IMyInterface x = MyCOMClass.GetData();
IMyOtherInterface y = (IMyOtherInterface)x;
IMyInterface z = x;
Marshal.ReleaseComObject(x);
Marshal.ReleaseComObject(y);
或者使用Marshal.FinalReleaseComObject(x); // 如果你知道没有其他人在使用它