ClassInterfaceAttribute 類別

定義

表示要為公開至 COM 之類別所產生的類別介面類型 (如果產生介面的話)。

public ref class ClassInterfaceAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class, Inherited=false)]
public sealed class ClassInterfaceAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ClassInterfaceAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class, Inherited=false)>]
type ClassInterfaceAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ClassInterfaceAttribute = class
    inherit Attribute
Public NotInheritable Class ClassInterfaceAttribute
Inherits Attribute
繼承
ClassInterfaceAttribute
屬性

範例

下列範例示範如何使用 值AutoDispatch套用 ClassInterfaceAttributeClassInterfaceType ,這會產生 IDispatchMyClass介面。

using namespace System::Runtime::InteropServices;

[ClassInterface(ClassInterfaceType::AutoDispatch)]
public ref class MyClass
{
public:
   MyClass(){}

};
using System.Runtime.InteropServices;

[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MyClass
{
   public MyClass() {}
}
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDispatch)> _
Public Class SampleClass    
    ' Insert class members here.
End Class

備註

您可以將此屬性套用至元件或類別。

這個屬性會控制類型庫匯出工具 (Tlbexp.exe) 是否會自動產生屬性類別的類別介面。 類別介面具有與類別本身相同的名稱,但名稱前面加上底線。 公開時,類別介面會包含Managed類別的所有 public、非 static 成員,包括繼承自其基類的成員。 Managed 類別無法存取類別介面,而且不需要直接存取類別成員。 Tlbexp.exe 會產生類別介面的唯一介面標識碼 (IID) 。

類別介面可以是雙重或僅限分派的介面。 您可以選擇性地隱藏類別介面的產生,並改為提供自定義介面。 您可以藉由指定 System.Runtime.InteropServices.ClassInterfaceType 列舉成員來公開或隱藏類別介面。 當您套用 ClassInterfaceAttribute 至元件時,除非個別類別以自己的屬性覆寫設定,否則屬性會與元件中的所有類別有關。

雖然類別介面可排除明確定義每個類別的介面工作,但強烈建議不要在生產應用程式中使用它們。 雙重類別介面可讓客戶端系結至可能會隨著類別演進而變更的特定介面配置。 例如,請考慮將類別介面公開給 COM 用戶端的 Managed 類別。 類別的第一個版本包含 方法和 NorthSouth。 Unmanaged 用戶端可以系結至類別介面,該介面提供 North 作為類別介面中的第一個方法,並以第二種方法 South 的形式提供方法。 現在,請考慮下一個版本的 類別,這個版本在 方法NorthSouth之間插入了新的 方法East。 嘗試透過舊類別介面系結至新類別的 Unmanaged 用戶端,最後會在想要呼叫 方法 時呼叫 方法 East ,因為介面內的方法 South位置已變更。 此外,基類配置的任何變更也會影響所有衍生類別類別的類別介面配置。 直接系結至類別的 Managed 用戶端不會顯示相同的版本設定問題。 如需使用類別介面的特定指導方針,請參閱 COM 可呼叫包裝函式

Tlbimp.exe (類型連結庫匯入工具) 一律會套用至列舉成員匯入的類別ClassInterfaceType.None,以指出現有的 COM 類別永遠不會公開 Managed 介面。

建構函式

ClassInterfaceAttribute(ClassInterfaceType)

使用指定的 ClassInterfaceAttribute 列舉型別 (Enumeration) 成員,初始化 ClassInterfaceType 類別的新執行個體。

ClassInterfaceAttribute(Int16)

使用指定的 ClassInterfaceAttribute 列舉值,初始化 ClassInterfaceType 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得描述應該產生哪個型別之類別介面的 ClassInterfaceType 值。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱