如何释放后期绑定的COM对象?

Mar*_*arc 0 .net c# com release late-binding

我想我也必须释放后期绑定的COM对象.
但这是如何直接完成的?

在我的情况下,我使用C#中的以下代码来获取Google Earth的重点(简化):

Type oClassType = Type.GetTypeFromProgID("GoogleEarth.ApplicationGE");
object oGE = Activator.CreateInstance(oClassType);
object oCamera = oGE.GetType().InvokeMember("GetCamera", System.Reflection.BindingFlags.InvokeMethod, null, oGE, new object[] { 0 });
double dFocusLatitude = (double)oCamera.GetType().InvokeMember("FocusPointLatitude", System.Reflection.BindingFlags.GetProperty, null, oCamera, null);
double dFocusLongitude = (double)oCamera.GetType().InvokeMember("FocusPointLongitude", System.Reflection.BindingFlags.GetProperty, null, oCamera, null);
Run Code Online (Sandbox Code Playgroud)

那么如何在这种情况下释放相机和Google Earth对象呢?

cms*_*sjr 6

您可以使用Marshal.ReleaseComObject

例如

if(Marshal.IsComObject(oGE)==true)
{
  Marshal.ReleaseComObject(oGE);
}
Run Code Online (Sandbox Code Playgroud)