ComVisibleAttribute 类

控制程序集中个别托管类型、成员或所有类型对 COM 的可访问性。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Enum Or AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Interface Or AttributeTargets.Delegate, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ComVisibleAttribute
    Inherits Attribute
用法
Dim instance As ComVisibleAttribute
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Interface|AttributeTargets.Delegate, Inherited=false)] 
[ComVisibleAttribute(true)] 
public sealed class ComVisibleAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Enum|AttributeTargets::Method|AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Interface|AttributeTargets::Delegate, Inherited=false)] 
[ComVisibleAttribute(true)] 
public ref class ComVisibleAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Interface|AttributeTargets.Delegate, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ComVisibleAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Interface|AttributeTargets.Delegate, Inherited=false) 
ComVisibleAttribute(true) 
public final class ComVisibleAttribute extends Attribute

备注

可将该属性应用于程序集、接口、类、结构、委托、枚举、字段、方法或属性。

默认为 true,指示该托管类型对 COM 是可见的。使所有公共托管程序集及类型可见并不需要使用此属性;默认情况下,它们对 COM 是可见的。只能使 public 类型可见。而不能使用该属性使原本为 internalprotected 的类型对 COM 可见,也不能使不可见类型的成员可见。

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

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

有关导出过程的详细说明,请参见 有关从程序集转换到类型库的摘要

示例

下面的示例演示了如何控制类对 COM 的可见性,使该类的成员不可见。通过在 MyClass 上将 ComVisibleAttribute 设置为 false,在 MyMethodMyProperty 上设置为 false,可以避免无意中通过继承向 COM 公开成员。

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
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;
      }
   }
}
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;
      }

   }

};
import System.Runtime.InteropServices.*;

/** @attribute ComVisible(false)
 */
class MyClass
{
    public MyClass()
    {
        //Insert code here.
    } //MyClass
   
    /** @attribute ComVisible(false)
     */
    public int MyMethod(String param)
    {
        return 0;
    } //MyMethod

    public boolean MyOtherMethod()
    {
        return true;
    } //MyOtherMethod

    /** @attribute ComVisible(false)
     */
    /** @property 
     */
    public int get_MyProperty()
    {
        return get_MyProperty();
    }//get_MyProperty
} //MyClass
import System.Runtime.InteropServices;

ComVisible(false) class MyClass
{
   public function MyClass()
   {
      //Insert code here.
   }
   
   ComVisible(false) public function MyMethod(param : String) : int 
   {
      return 0;
   }

   public function MyOtherMethod() : boolean
   {
      return true;
   }

   ComVisible(false) public function get MyProperty() : int 
   {
       return MyProperty;
   }
}

继承层次结构

System.Object
   System.Attribute
    System.Runtime.InteropServices.ComVisibleAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ComVisibleAttribute 成员
System.Runtime.InteropServices 命名空间

其他资源

类型库导出程序 (Tlbexp.exe)
有关从程序集转换到类型库的摘要