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 類別的所有 publicstatic 成員,包括繼承自其基類的成員。 Managed 類別無法存取類別介面,而且不需要直接存取類別成員。 Tlbexp.exe會產生類別介面的唯一介面識別碼 (IID) 。

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

雖然類別介面可排除明確定義每個類別介面的工作,但強烈建議不要在生產應用程式中使用它們。 雙重類別介面可讓用戶端系結至特定介面配置,這些配置可能會隨著類別演進而變更。 例如,請考慮將類別介面公開給 COM 用戶端的 Managed 類別。 類別的第一個版本包含 方法和 NorthSouth 。 Unmanaged 用戶端可以系結至類別介面,以提供 作為類別介面中的第一個方法,並以 South 第二個方法的形式提供 North 。 現在,請考慮類別的下一個版本,其具有新的 方法 , East 在 方法 NorthSouth 之間插入。 嘗試透過舊類別介面系結至新類別的 Unmanaged 用戶端,最後會在想要呼叫 方法時呼叫 方法 EastSouth ,因為介面內的方法位置已變更。 此外,基類配置的任何變更也會影響所有衍生類別的類別介面配置。 直接系結至類別的 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)

適用於

另請參閱