通过


ComVisibleAttribute 类

定义

控制单个托管类型或成员或程序集中所有类型的辅助功能到 COM。

public ref class ComVisibleAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
public sealed class ComVisibleAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ComVisibleAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
type ComVisibleAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ComVisibleAttribute = class
    inherit Attribute
Public NotInheritable Class ComVisibleAttribute
Inherits Attribute
继承
ComVisibleAttribute
属性

示例

以下示例演示如何控制类 COM 的可见性,以便其成员不可见。 通过将 ComVisibleAttribute 设置为 MyClass(Visual Basic 示例中的 SampleClass)上的 false,并在 MyMethodMyPropertyfalse,可以避免无意中通过继承向 COM 公开成员。

using namespace System::Runtime::InteropServices;

[ComVisible(false)]
ref class MyClass
{
private:
   int myProperty;

public:
   MyClass()
   {
      
      //Insert code here.
   }


   [ComVisible(false)]
   int MyMethod( String^ param )
   {
      return 0;
   }

   bool MyOtherMethod()
   {
      return true;
   }


   property int MyProperty 
   {

      [ComVisible(false)]
      int get()
      {
         return myProperty;
      }

   }

};
using System.Runtime.InteropServices;

[ComVisible(false)]
class MyClass
{
   public MyClass()
   {
      //Insert code here.
   }

   [ComVisible(false)]
   public int MyMethod(string param)
   {
      return 0;
   }

   public bool MyOtherMethod()
   {
      return true;
   }

   [ComVisible(false)]
   public int MyProperty
   {
      get
      {
         return MyProperty;
      }
   }
}
Imports System.Runtime.InteropServices

<ComVisible(False)> _
Class SampleClass
    
    Public Sub New()
        'Insert code here.
    End Sub
    
    <ComVisible(False)> _
    Public Function MyMethod(param As String) As Integer
        Return 0
    End Function    
    
    Public Function MyOtherMethod() As Boolean
        Return True
    End Function
    
    <ComVisible(False)> _
    Public ReadOnly Property MyProperty() As Integer
        Get
            Return MyProperty
        End Get
    End Property
    
End Class

注解

可以将此属性应用于程序集、接口、类、结构、委托、枚举、字段、方法、事件访问器或属性。

默认值 true为,指示托管类型对 COM 可见。 无需此属性即可使公共托管程序集和类型可见;默认情况下,COM 可以看到它们。 只能 public 使类型可见。 该属性不能用于使 COM 或 internal 类型对 COM 可见或 protected 使非可见类型的成员可见。

将属性设置为 false 程序集会 public 隐藏程序集中的所有类型。 可以通过将各个类型设置为 来选择性地使程序集中的类型可见。 将特性设置为 false 特定类型会隐藏该类型及其成员。 但是,如果类型不可见,则无法使类型的成员可见。 将属性设置为 false 类型可防止该类型导出到类型库;未注册类;接口永远不会响应非托管 QueryInterface 调用。

除非显式将类及其成员 false设置为,否则继承的类可以向原始类中不可见的 COM 基类成员公开。 例如,如果将 ClassA 设置为 false 并且不将其属性应用于其成员,则类及其成员对 COM 不可见。 但是,如果从 ClassA 派生 ClassB 并将 ClassB 导出到 COM,ClassA 成员将成为 ClassB 的可见基类成员。

构造函数

名称 说明
ComVisibleAttribute(Boolean)

初始化 ComVisibleAttribute 类的新实例。

属性

名称 说明
TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)
Value

获取一个值,该值指示 COM 类型是否可见。

方法

名称 说明
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)

适用于

另请参阅