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)]
public sealed class ClassInterfaceAttribute : Attribute
type ClassInterfaceAttribute = class
    inherit Attribute
Public NotInheritable Class ClassInterfaceAttribute
Inherits Attribute


下面的示例演示如何应用ClassInterfaceAttributeClassInterfaceTypeAutoDispatch,以生成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;

public ref class MyClass


using System.Runtime.InteropServices;

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托管类,包括成员的成员继承自它的基类。When exposed, the class interface contains all the public, non- static members of the managed class, including members inherited from its base class. 托管的类不能访问的类接口而不需要为如可以直接访问类成员。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 客户端的托管的类。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. 非托管客户端可以将绑定到类接口,它提供了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. 尝试将绑定到通过旧的类接口的新类的非托管的客户端在调用方法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 类从不公开托管接口。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)

使用指定的 ClassInterfaceType 枚举成员初始化 ClassInterfaceAttribute 类的新实例。Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration member.

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

使用指定的 ClassInterfaceType 枚举值初始化 ClassInterfaceAttribute 类的新实例。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 的浅表副本。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)

将一组名称映射为对应的一组调度标识符。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)