ClassInterfaceAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示在完全生成某个接口后为要向 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 AttributePublic NotInheritable Class ClassInterfaceAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例演示如何应用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) 是否自动生成特性化类的类接口。 类接口携带与类本身相同的名称,但名称带有下划线前缀。 公开时,类接口包含托管类的所有 public非 static 成员,包括从其基类继承的成员。 托管类无法访问类接口,并且无需访问,因为它们可以直接访问类成员。 Tlbexp.exe 为类接口生成唯一的接口标识符 (IID) 。
类接口可以是双接口,也可以是仅调度接口。 (可选)可以禁止生成类接口,改为提供自定义接口。 通过指定 System.Runtime.InteropServices.ClassInterfaceType 枚举成员来公开或禁止类接口。 应用于 ClassInterfaceAttribute 程序集时, 属性与程序集中的所有类相关,除非各个类使用自己的属性替代设置。
尽管类接口无需为每个类显式定义接口,但强烈建议不要在生产应用程序中使用它们。 双类接口允许客户端绑定到特定的接口布局,该布局可能会随着类的发展而更改。 例如,考虑向 COM 客户端公开类接口的托管类。 类的第一个版本包含 方法和 NorthSouth。 非托管客户端可以绑定到类接口,该接口提供 North 作为类接口中的第一个方法,将 方法 South 作为第二种方法提供。 现在考虑类的下一个版本,该类在 方法East和 South之间插入了一个新方法 North 。 尝试通过旧类接口绑定到新类的非托管客户端在打算调用 方法 East 时,最终会调用 方法 South,因为接口中方法的位置已更改。 此外,对基类布局的任何更改也会影响所有派生类的类接口布局。 直接绑定到类的托管客户端不会出现相同的版本控制问题。 有关使用类接口的具体准则,请参阅 COM 可调用包装器。
Tlbimp.exe (类型库导入程序) 始终应用于导入的ClassInterfaceType.None类枚举成员,以指示现有 COM 类永远不会公开托管接口。
构造函数
| ClassInterfaceAttribute(ClassInterfaceType) | 使用指定的 ClassInterfaceType 枚举成员初始化 ClassInterfaceAttribute 类的新实例。 | 
| ClassInterfaceAttribute(Int16) | 使用指定的 ClassInterfaceType 枚举值初始化 ClassInterfaceAttribute 类的新实例。 | 
属性
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
| Value | 获取 ClassInterfaceType 值,该值描述应为该类生成的接口的类型。 | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |