ComVisibleAttribute 类

定义

控制程序集中单独托管类型或成员,或所有类型对 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.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
继承
ComVisibleAttribute
属性

示例

下面的示例演示了如何控制类的 COM 可见性,使其成员不可见。The following example shows how you can control the visibility to COM of a class so that its members are invisible. 通过将ComVisibleAttribute设置falseMyClasson(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. 属性不能用于使internal对 COM 可见或protected类型可见,或使不可见类型的成员可见。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. 可以通过将各个类型设置为, true在程序集中有选择地使类型可见。You 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 类的新实例。Initializes a new instance of the ComVisibleAttribute class.

属性

TypeId

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

(继承自 Attribute)
Value

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

方法

Equals(Object)

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

(继承自 Attribute)
GetHashCode()

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

(继承自 Attribute)
GetType()

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

(继承自 Object)
IsDefaultAttribute()

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

(继承自 Attribute)
Match(Object)

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

(继承自 Attribute)
MemberwiseClone()

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

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

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

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

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

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

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

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

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(继承自 Attribute)

适用于

另请参阅