ComVisibleAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
控制程序集中单独托管类型或成员,或所有类型对 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 AttributePublic NotInheritable Class ComVisibleAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例演示如何控制类 COM 的可见性,使其成员不可见。 通过在 Visual Basic 示例中)  (SampleClass 上将 设置为 ComVisibleAttributeMyClassfalse ,在 false 和 MyProperty上MyMethod,可以避免无意中通过继承向 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 使类型可见。 特性不能用于使 其他或internalprotected类型对 COM 可见,或使不可见类型的成员可见。
将程序集上的 属性设置为 false 会 public 隐藏程序集中的所有类型。 可以通过将各个类型设置为 ,有选择地使程序集中的类型 true可见。 在特定类型上将 属性设置为 false 会隐藏该类型及其成员。 但是,如果类型不可见,则无法使类型的成员可见。 在类型上将 属性设置为 false 可防止将该类型导出到类型库;类未注册;接口永远不会响应非托管 QueryInterface 调用。
除非将类及其成员显式设置为 false,否则继承的类可以向在原始类中不可见的 COM 基类成员公开。 例如,如果将 ClassA 设置为 false ,但不将特性应用于其成员,则类及其成员对 COM 不可见。 但是,如果从 ClassA 派生 ClassB 并将 ClassB 导出到 COM,则 ClassA 成员将成为 ClassB 的可见基类成员。
有关导出过程的详细说明,请参阅 程序集到类型库转换摘要。
构造函数
| ComVisibleAttribute(Boolean) | 初始化  | 
属性
| 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) |