ClassInterfaceType 列舉

定義

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

public enum class ClassInterfaceType
public enum ClassInterfaceType
[System.Serializable]
public enum ClassInterfaceType
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ClassInterfaceType
type ClassInterfaceType = 
[<System.Serializable>]
type ClassInterfaceType = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ClassInterfaceType = 
Public Enum ClassInterfaceType
繼承
ClassInterfaceType
屬性

欄位

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 使用。

using namespace System;
using namespace 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 ref class AClassUsableViaCOM
{
public:
    AClassUsableViaCOM() 
    { 
    }

public:
    int Add(int x, int 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 ref class AnotherClassUsableViaCOM : public IComparable
{
public:
    AnotherClassUsableViaCOM() 
    { 
    }

    virtual int CompareTo(Object^ o) = IComparable::CompareTo
    {
        return 0;
    }
};
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; }
}
Imports 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 Sub New()

    End Sub

    Public Function Add(ByVal x As Int32, ByVal y As Int32) As Int32
        Return x + y

    End Function
End Class
' 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
    Implements IComparable

    Public Sub New()

    End Sub

    Function CompareTo(ByVal o As [Object]) As Int32 Implements IComparable.CompareTo
        Return 0

    End Function 'IComparable.CompareTo
End Class

備註

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

適用於

另請參閱