ClassInterfaceAttribute ClassInterfaceAttribute ClassInterfaceAttribute ClassInterfaceAttribute Class

定義

表示要為公開至 COM 之類別所產生的類別介面類型 (如果產生介面的話)。Indicates the type of class interface to be generated for a class exposed to COM, if an interface is generated at all.

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

範例

下列範例示範如何套用ClassInterfaceAttribute具有ClassInterfaceTypeAutoDispatch,以產生IDispatch介面MyClassThe following example shows how to apply the ClassInterfaceAttribute with the ClassInterfaceType value AutoDispatch, which generates an IDispatch interface for MyClass.

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

備註

您可以套用這個屬性的組件或類別。You can apply this attribute to assemblies or classes.

這個屬性會控制型別程式庫匯出工具 (Tlbexp.exe) 是否會自動產生類別介面屬性化類別。This attribute controls whether the Type Library Exporter (Tlbexp.exe) automatically generates a class interface for the attributed class. 類別介面同名類別本身,但名稱前面會加上底線。A class interface carries the same name as the class itself, but the name is prefixed with an underscore. 類別介面公開時,包含所有public、 非- static managed 類別,包括成員的成員繼承自其基底類別。When exposed, the class interface contains all the public, non- static members of the managed class, including members inherited from its base class. Managed 的類別無法存取類別介面,且有不需要,因為它們可以直接存取類別成員。Managed classes cannot access a class interface and have no need to as they can access the class members directly. Tlbexp.exe 會產生類別介面的唯一的介面識別碼 (IID)。Tlbexp.exe generates a unique interface identifier (IID) for the class interface.

類別介面可以是雙重或僅分派介面。Class interfaces can be dual or dispatch-only interfaces. (選擇性) 您可以隱藏產生類別介面,並改為提供一種自訂介面。Optionally, you can suppress the generation of the class interface and provide a custom interface instead. 您將公開或隱藏指定的類別介面System.Runtime.InteropServices.ClassInterfaceType列舉成員。You expose or suppress a class interface by specifying a System.Runtime.InteropServices.ClassInterfaceType enumeration member. 當您將套用ClassInterfaceAttribute組件屬性屬於組件中的所有類別除非個別類別覆寫的設定,以自己的屬性。When you apply ClassInterfaceAttribute to an assembly, the attribute pertains to all classes in the assembly unless the individual classes override the setting with their own attribute.

雖然類別介面消除明確定義為每個類別的介面的工作,其用於生產應用程式極為不妥。Although class interfaces eliminate the task of explicitly defining interfaces for each class, their use in production applications is strongly discouraged. 雙重類別介面允許用戶端繫結至特定的介面配置隨著類別的發展,有可能變更。Dual class interfaces allow clients to bind to a specific interface layout that is subject to change as the class evolves. 例如,請考慮公開給 COM 用戶端類別介面的 managed 的類別。For example, consider a managed class that exposes a class interface to COM clients. 第一個版本的類別包含方法NorthSouthThe first version of the class contains methods North and South. Unmanaged 用戶端可以繫結至類別介面,可提供North中的類別介面和方法的第一個方法為South做為第二個方法。An unmanaged client can bind to the class interface, which provides North as the first method in the class interface and method South as the second method. 現在,請考慮的類別,有新的方法,下一版East中,插入方法之間NorthSouthNow consider the next version of the class, which has a new method, East, inserted between methods North and South. 嘗試繫結至新的類別,透過舊的類別介面的 unmanaged 用戶端最後會呼叫方法East他們想要呼叫方法的當South,因為在介面中方法的位置已變更。Unmanaged clients that try to bind to the new class through the old class interface end up calling method East when they intend to call method South, because the positioning of methods within the interface has changed. 此外,對基底類別的配置的任何變更也會影響所有衍生類別的類別介面的版面配置。Moreover, any change to the layout of a base class also affects the layout of the class interface for all derived classes. Managed 用戶端的繫結直接到類別,並不會出現相同的版本控制問題。Managed clients, which bind directly to classes, do not exhibit the same versioning problems. 如需使用類別介面的特定指導方針,請參閱 < COM 可呼叫包裝函式For specific guidelines on using a class interface, see COM Callable Wrapper.

Tlbimp.exe (型別程式庫匯入工具)一律會套用到匯入類別ClassInterfaceType.None列舉成員,表示現有的 COM 類別永遠不會公開管理介面。The Tlbimp.exe (Type Library Importer) always applies to imported classes the ClassInterfaceType.None enumeration member to indicate that existing COM classes never expose managed interfaces.

建構函式

ClassInterfaceAttribute(ClassInterfaceType) ClassInterfaceAttribute(ClassInterfaceType) ClassInterfaceAttribute(ClassInterfaceType) ClassInterfaceAttribute(ClassInterfaceType)

使用指定的 ClassInterfaceAttribute 列舉型別 (Enumeration) 成員,初始化 ClassInterfaceType 類別的新執行個體。Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration member.

ClassInterfaceAttribute(Int16) ClassInterfaceAttribute(Int16) ClassInterfaceAttribute(Int16) ClassInterfaceAttribute(Int16)

使用指定的 ClassInterfaceAttribute 列舉值,初始化 ClassInterfaceType 類別的新執行個體。Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration value.

屬性

TypeId TypeId TypeId TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value Value Value Value

取得描述應該產生哪個型別之類別介面的 ClassInterfaceType 值。Gets the ClassInterfaceType value that describes which type of interface should be generated for the class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

適用於

另請參閱