ComVisibleAttribute ComVisibleAttribute ComVisibleAttribute ComVisibleAttribute Class

定義

控制公開至 COM 之個別 Managed 類型或成員,或組件內所有類型的存取範圍。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,這表示 managed 型別為 com 可見The default is true, which indicates that the managed type is visible to COM. 若要顯示公用的 managed 組件和類型; 不需要這個屬性它們是預設為 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. 例如,如果您設定 ClassAfalse並不適用於其成員的屬性、 類別和其成員會看不到 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. 不過,如果您是 ClassB 衍生自 ClassA,並匯出至 COM 的 ClassB,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 的淺層複本 (Shallow Copy)。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱