ClassInterfaceType 枚举

定义

标识为类生成的类接口类型。

public enum class ClassInterfaceType
public enum ClassInterfaceType
[System.Serializable]
public enum ClassInterfaceType
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ClassInterfaceType
type ClassInterfaceType = 
[<System.Serializable>]
type ClassInterfaceType = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ClassInterfaceType = 
Public Enum ClassInterfaceType
继承
ClassInterfaceType
属性

字段

AutoDispatch 1

指示该类只支持 COM 客户端的后期绑定。 在请求时,该类的 dispinterface 会自动向 COM 客户端公开。 Tlbexp.exe(类型库导出程序) 生成的类型库不包含 dispinterface 的类型信息,以防止客户端缓存接口的 DISPID。 由于客户端只能后期绑定到接口,因此 dispinterface 不会出现 ClassInterfaceAttribute 中所述的版本控制问题。

这是 ClassInterfaceAttribute 的默认设置。

AutoDual 2

指示自动为类生成双重类接口并向 COM 公开。 为该类接口生成类型信息并在类型库中发布。 由于 ClassInterfaceAttribute 中描述的版本控制方面的限制,极力建议不要使用 AutoDual

None 0

指示不为类生成类接口。 如果未显式实现任何接口,则该类只能通过 IDispatch 接口提供后期绑定访问。 这是 ClassInterfaceAttribute 的推荐设置。 要通过由类显式实现的接口公开功能,唯一的方法是使用 ClassInterfaceType.None

Tlbexp.exe (类型库导出程序)公开作为组件类的默认接口由类实现的第一个公共、COM 可见接口。 在 .NET Framework 2.0 及更高版本中,可以使用 ComDefaultInterfaceAttribute 属性指定向 COM 公开的默认接口。 如果类未实现任何接口,则由基类实现的第一个公共的、COM 可见的接口将成为默认接口(从最近派生的基类开始,以此倒推)。 如果类及其基类都不实现接口,则 Tlbexp.exe 会将 _Object 公开为默认接口。

示例

此示例演示如何将类型应用于 ClassInterfaceAttribute 设置 ClassInterfaceType。 可通过非托管 COM 使用以这种方式定义的类。

using namespace System;
using namespace System::Runtime::InteropServices;

// Have the CLR expose a class interface (derived from IDispatch)
// for this type. COM clients can call the  members of this
// class using the Invoke method from the IDispatch interface.
[ClassInterface(ClassInterfaceType::AutoDispatch)]
public ref class AClassUsableViaCOM
{
public:
    AClassUsableViaCOM() 
    { 
    }

public:
    int Add(int x, int y)
    {
        return x + y;
    }
};

// The CLR does not expose a class interface for this type.
// COM clients can call the members of this class using
// the methods from the IComparable interface.
[ClassInterface(ClassInterfaceType::None)]
public ref class AnotherClassUsableViaCOM : public IComparable
{
public:
    AnotherClassUsableViaCOM() 
    { 
    }

    virtual int CompareTo(Object^ o) = IComparable::CompareTo
    {
        return 0;
    }
};
using System;
using System.Runtime.InteropServices;

// Have the CLR expose a class interface (derived from IDispatch) for this type.
// COM clients can call the  members of this class using the Invoke method from the IDispatch interface.
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class AClassUsableViaCOM
{
    public AClassUsableViaCOM() { }

    public Int32 Add(Int32 x, Int32 y) { return x + y; }
}

// The CLR does not expose a class interface for this type.
// COM clients can call the members of this class using the methods from the IComparable interface.
[ClassInterface(ClassInterfaceType.None)]
public class AnotherClassUsableViaCOM : IComparable
{
    public AnotherClassUsableViaCOM() { }

    Int32 IComparable.CompareTo(Object o) { return 0; }
}
Imports System.Runtime.InteropServices


' Have the CLR expose a class interface (derived from IDispatch) for this type.
' COM clients can call the  members of this class using the Invoke method from the IDispatch interface.
<ClassInterface(ClassInterfaceType.AutoDispatch)> _
Public Class AClassUsableViaCOM

    Public Sub New()

    End Sub

    Public Function Add(ByVal x As Int32, ByVal y As Int32) As Int32
        Return x + y

    End Function
End Class
' The CLR does not expose a class interface for this type.
' COM clients can call the members of this class using the methods from the IComparable interface.
<ClassInterface(ClassInterfaceType.None)> _
Public Class AnotherClassUsableViaCOM
    Implements IComparable

    Public Sub New()

    End Sub

    Function CompareTo(ByVal o As [Object]) As Int32 Implements IComparable.CompareTo
        Return 0

    End Function 'IComparable.CompareTo
End Class

注解

此枚举与 ClassInterfaceAttribute 特性结合使用。

适用于

另请参阅