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
屬性

範例

以下範例展示了如何將 應用ClassInterfaceAttribute為 的值AutoDispatch,從而產生IDispatch一個 的MyClass介面。ClassInterfaceType

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)是否自動為該類別產生類別介面。 類別介面與類別本身名稱相同,但名稱前會有底線。 當類別介面被公開時,包含所有 publicstatic 受管理類別的成員,包括從其基底類別繼承的成員。 受管理類別無法存取類別介面,也不需要,因為它們可以直接存取類別成員。 Tlbexp.exe 會為類別介面產生唯一的介面識別碼(IID)。

類別介面可以是雙重介面或僅派遣介面。 你也可以選擇不產生職業介面,並提供自訂介面。 你可以透過指定 System.Runtime.InteropServices.ClassInterfaceType 列舉成員來暴露或抑制類別介面。 當你套用 ClassInterfaceAttribute 一個組合語言時,該屬性會涵蓋該組合中的所有類別,除非各個類別用自己的屬性覆蓋設定。

雖然類別介面省去了為每個類別明確定義介面的任務,但在生產環境中強烈不建議使用。 雙重類別介面允許用戶端綁定到特定介面配置,且該介面配置會隨著類別演進而改變。 舉例來說,考慮一個受管理類別,該類別向 COM 用戶端開放一個介面。 該類別的第一個版本包含方法 NorthSouth。 非受管理用戶端可以綁定到類別介面,類別介面作為類別介面中的第一個方法,第二個方法則是 North 方法 South 。 現在考慮該類別的下一個版本,其中在方法NorthSouth之間插入了一個新方法,East。 未受管理的客戶端若嘗試透過舊類別介面綁定到新類別,結果會在原本打算呼叫方法South時呼叫方法East,因為介面中方法的位置已經改變。 此外,基底類別的佈局任何變更也會影響所有衍生類別的類別介面佈局。 直接綁定類別的受管理用戶端則不會出現相同的版本管理問題。 關於使用類別介面的具體指引,請參見 COM 可呼叫包裝器

Tlbimp.exe(類型庫匯入器)總是套用於匯入的類別,列ClassInterfaceType.None舉成員用以表示現有的 COM 類別永遠不會暴露受管理介面。

建構函式

名稱 Description
ClassInterfaceAttribute(ClassInterfaceType)

初始化一個新的類別實例 ClassInterfaceAttribute ,並使用指定的 ClassInterfaceType 列舉成員。

ClassInterfaceAttribute(Int16)

初始化一個新的類別實例 ClassInterfaceAttribute ,並以指定的 ClassInterfaceType 列舉值。

屬性

名稱 Description
TypeId

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

(繼承來源 Attribute)
Value

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

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱