ClassInterfaceType 列舉

定義

識別為類別所產生的類別介面類型。

C#
public enum ClassInterfaceType
C#
[System.Serializable]
public enum ClassInterfaceType
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ClassInterfaceType
繼承
ClassInterfaceType
屬性

欄位

名稱 Description
AutoDispatch 1

指示類別對 COM 用戶端只支援晚期繫結 (Late Binding)。 類別的 dispinterface 應要求會自動地公開至 COM 用戶端。 Tlbexp.exe (型別程式庫匯出工具) 產生的型別程式庫不包含 dispinterface 類型資訊,以免用戶端快取介面的 DISPID。 dispinterface 不會出現 ClassInterfaceAttribute 中所描述的版本控制問題,因為用戶端只能晚期繫結到介面。

這是 ClassInterfaceAttribute 的預設設定。

AutoDual 2

指示雙重類別介面自動地產生給類別並公開至 COM。 型別資訊產生給類別介面並發行至型別程式庫。 由於 ClassInterfaceAttribute 所描述的版本控制限制,我們非常不鼓勵使用 AutoDual

None 0

指示沒有類別介面產生給類別。 如果沒有明確實作介面,類別只能透過 IDispatch 介面提供晚期繫結存取。 這是 ClassInterfaceAttribute 的建議預設值。 使用 ClassInterfaceType.None 是唯一透過類別明確實作的介面公開功能的方法。

Tlbexp.exe (類型庫導出工具) 公開 類別所實作的第一個公用 COM 可見介面作為 coclass 的預設介面。 在 .NET Framework 2.0 和更新版本中,您可以使用 屬性來指定公開給 COM ComDefaultInterfaceAttribute 的預設介面。 如果類別未實作任何介面,基類所實作的第一個公用 COM 可見介面會變成預設介面, (從最近衍生的基類開始,並回溯運作) 。 如果類別或其基類都實作介面,Tlbexp.exe 會公開 _Object 為預設介面。

範例

此範例示範如何將 套用 ClassInterfaceAttribute 至 型別,並設定 ClassInterfaceType。 以這種方式定義的類別可以從 Unmanaged COM 使用。

C#
using System;
using System.Runtime.InteropServices;

// Have the CLR expose a class interface (derived from IDispatch) for this type.
// COM clients can call the  members of this class using the Invoke method from the IDispatch interface.
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class AClassUsableViaCOM
{
    public AClassUsableViaCOM() { }

    public Int32 Add(Int32 x, Int32 y) { return x + y; }
}

// The CLR does not expose a class interface for this type.
// COM clients can call the members of this class using the methods from the IComparable interface.
[ClassInterface(ClassInterfaceType.None)]
public class AnotherClassUsableViaCOM : IComparable
{
    public AnotherClassUsableViaCOM() { }

    Int32 IComparable.CompareTo(Object o) { return 0; }
}

備註

這個列舉會與 屬性搭配 ClassInterfaceAttribute 使用。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱