ClassInterfaceAttribute 類別

定義

表示要為公開至 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
繼承
ClassInterfaceAttribute
屬性

範例

下列範例ClassInterfaceAttribute示範如何ClassInterfaceType使用AutoDispatch值來MyClass套用, 這會產生的介面。IDispatchThe 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. 公開時, 類別介面包含 managed 類別的public所有static非成員, 包括繼承自其基類的成員。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. 類別的第一個版本包含方法North和。 SouthThe first version of the class contains methods North and South. 未受管理的用戶端可以系結至類別介面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, 並在方法NorthSouth之間插入。Now consider the next version of the class, which has a new method, East, inserted between methods North and South. 未受管理的用戶端會嘗試透過舊的類別介面系結至新類別, 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 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 類別永遠不會公開 managed 介面。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 列舉型別 (Enumeration) 成員,初始化 ClassInterfaceType 類別的新執行個體。Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration member.

ClassInterfaceAttribute(Int16)

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

屬性

TypeId

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

(繼承來源 Attribute)
Value

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

方法

Equals(Object)

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

(繼承來源 Attribute)
GetHashCode()

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

(繼承來源 Attribute)
GetType()

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

(繼承來源 Object)
IsDefaultAttribute()

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

(繼承來源 Attribute)
Match(Object)

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

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

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

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

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

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

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

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

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

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

(繼承來源 Attribute)

適用於

另請參閱