[英]CreateObject in C# to use a COM component
I have in my project a COM Interop where I use this code to make the call.我的项目中有一个 COM Interop,我使用此代码进行调用。
object oCOM = CreateObject("SystemInterface.cApplication");
I don't think this way is better in C# code.我不认为这种方式在 C# 代码中更好。
A Simple change:一个简单的改变:
object oCOM = null;
try
{
var oType = Type.GetTypeFromProgID("SystemInterface.cApplication");
oCOM = Activator.CreateInstance(oType);
}
catch(Exception)
{
throw;
}
finally
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oCOM);
}
It is very important how you dispose of the object, be sure to use the code that is in "finally".如何处理 object 非常重要,请务必使用“finally”中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.