簡體   English   中英

C# 中的 CreateObject 使用 COM 組件

[英]CreateObject in C# to use a COM component

我的項目中有一個 COM Interop,我使用此代碼進行調用。

 object oCOM = CreateObject("SystemInterface.cApplication");

我不認為這種方式在 C# 代碼中更好。

一個簡單的改變:

object oCOM = null;

        try
        {

            var oType = Type.GetTypeFromProgID("SystemInterface.cApplication");
            oCOM = Activator.CreateInstance(oType);

        }
        catch(Exception)
        {
            throw;
        }
        finally
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oCOM);
        }

如何處理 object 非常重要,請務必使用“finally”中的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM