ComVisibleAttribute ComVisibleAttribute ComVisibleAttribute ComVisibleAttribute Class

定义

控制程序集中单独托管类型或成员,或所有类型对 COM 的访问性。Controls accessibility of an individual managed type or member, or of all types within an assembly, to COM.

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

示例

下面的示例演示如何控制对类的 COM 可见性,以便其成员不可见。The following example shows how you can control the visibility to COM of a class so that its members are invisible. 通过设置ComVisibleAttributefalseMyClass(SampleClass在 Visual Basic 示例),并且falseMyMethodMyProperty,可以避免无意中公开到 COM 通过继承的成员。By setting ComVisibleAttribute to false on MyClass(SampleClass in the Visual Basic example), and false on MyMethod and MyProperty, you can avoid inadvertently exposing the members to COM through inheritance.

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

注解

可以将此特性应用于程序集、 接口、 类、 结构、 委托、 枚举、 字段、 方法或属性。You can apply this attribute to assemblies, interfaces, classes, structures, delegates, enumerations, fields, methods, or properties.

默认值是true,指示托管的类型是对 COM 可见。The default is true, which indicates that the managed type is visible to COM. 使公共托管程序集和类型可见,则不需要此属性它们是默认情况下对 COM 可见。This attribute is not needed to make public managed assemblies and types visible; they are visible to COM by default. public类型可以为可见。Only public types can be made visible. 该属性不能用于使原本internalprotected类型对 COM 或以便显示不可见的类型的成员可见。The attribute cannot be used to make an otherwise internal or protected type visible to COM or to make members of a nonvisible type visible.

将属性设置为false程序集上隐藏所有public集中的类型。Setting the attribute to false on the assembly hides all public types within the assembly. 您可以有选择地使类型程序集内的可见通过将各个类型设置为trueYou can selectively make types within the assembly visible by setting the individual types to true. 将属性设置为false特定类型上隐藏该类型及其成员。Setting the attribute to false on a specific type hides that type and its members. 但是,您不能使类型的成员的可见类型是否为不可见。However, you cannot make members of a type visible if the type is invisible. 将属性设置为false上一种类型可防止该类型导出到类型库; 类没有注册; 接口是永远不会对非托管的响应能力QueryInterface调用。Setting the attribute to false on a type prevents that type from being exported to a type library; classes are not registered; interfaces are never responsive to unmanaged QueryInterface calls.

除非显式设置的类和其成员false、 继承的类可以向 COM 公开基类成员的原始类中不可见。Unless you explicitly set a class and its members to false, inherited classes can expose to COM base class members that are invisible in the original class. 例如,如果 ClassA 设置为false和未将特性应用于其成员,类及其成员是否对 COM 不可见For example, if you set ClassA to false and do not apply the attribute to its members, the class and its members are invisible to COM. 但是,如果 ClassA 派生自 ClassB,ClassB 导出到 COM ClassA 成员变为 ClassB 的可见基类成员。However, if you derive ClassB from ClassA and export ClassB to COM, ClassA members become visible base class members of ClassB.

导出过程的详细说明,请参阅程序集到类型库转换摘要For a detailed description of the export process, see Assembly to Type Library Conversion Summary.

构造函数

ComVisibleAttribute(Boolean) ComVisibleAttribute(Boolean) ComVisibleAttribute(Boolean) ComVisibleAttribute(Boolean)

初始化 ComVisibleAttribute 类的新实例。Initializes a new instance of the ComVisibleAttribute class.

属性

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value Value Value Value

获取一个值,该值指示 COM 类型是否可见。Gets a value that indicates whether the COM type is visible.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于

另请参阅