ClassInterfaceAttribute 类

定义

指示在完全生成某个接口后为要向 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 Attribute
Public NotInheritable Class ClassInterfaceAttribute
Inherits Attribute
继承
ClassInterfaceAttribute
属性

示例

以下示例演示如何应用 ClassInterfaceAttributeClassInterfaceTypeAutoDispatch,这将生成 IDispatch 接口 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

注解

可以将此属性应用于程序集或类。

此属性控制类型库导出程序 (Tlbexp.exe) 是否自动生成特性化类的类接口。 类接口与类本身的名称相同,但名称以下划线为前缀。 公开时,类接口包含托管类的所有 publicstatic 成员,包括从其基类继承的成员。 托管类无法访问类接口,并且无需访问,因为它们可以直接访问类成员。 Tlbexp.exe 为类接口生成唯一的接口标识符 (IID) 。

类接口可以是双接口,也可以是仅调度接口。 (可选)可以禁止生成类接口,并改为提供自定义接口。 通过指定 System.Runtime.InteropServices.ClassInterfaceType 枚举成员来公开或禁止显示类接口。 应用于 ClassInterfaceAttribute 程序集时,除非各个类使用自己的属性替代设置,否则 特性将适用于程序集中的所有类。

尽管类接口消除了为每个类显式定义接口的任务,但强烈建议不要在生产应用程序中使用它们。 双类接口允许客户端绑定到特定接口布局,该布局可能会随着类的发展而发生更改。 例如,请考虑向 COM 客户端公开类接口的托管类。 类的第一个版本包含 方法和 NorthSouth。 非托管客户端可以绑定到类接口,该接口提供 North 作为类接口中的第一个方法,将方法 South 作为第二个方法提供。 现在考虑下一个版本的 类,该类在 方法EastSouth之间插入了一个新方法 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)

适用于

另请参阅