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)]
public sealed class ClassInterfaceAttribute : 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,后者为 MyClass生成 IDispatch 接口。The 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. 类的第一个版本包含 NorthSouth的方法。The 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. 现在,请考虑类的下一个版本,该类具有在 NorthSouth的方法之间插入的新方法 EastNow consider the next version of the class, which has a new method, East, inserted between methods North and South. 尝试通过旧类接口绑定到新类的非托管客户端在要调用方法 SouthEast 结束调用方法,因为该接口中的方法的位置已更改。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 类的新实例。Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration member.


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



在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

获取 ClassInterfaceType 值,该值描述应为该类生成的接口的类型。Gets the ClassInterfaceType value that describes which type of interface should be generated for the class.



返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)


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

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

检索对象提供的类型信息接口的数量(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)