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
属性

示例

下面的示例演示如何应用ClassInterfaceAttribute ClassInterfaceType AutoDispatch值,该值生成MyClass接口。IDispatch

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 客户端公开类接口的托管类。 类的第一个版本包含方法和 North South。 非托管客户端可以绑定到类接口,该 North 接口提供类接口中的第一种方法,方法 South 作为第二种方法。 现在,请考虑下一个版本的类,该类有一个新方法,East在方法North``South与方法之间插入。 尝试通过旧类接口绑定到新类的非托管客户端在打算调用方法时最终会调用方法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)

适用于

另请参阅