ComVisibleAttribute 類別

定義

控制公開至 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.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. 藉由ComVisibleAttributefalseMyClass在VisualBasicfalse範例中)和MyMethod設定為on,您可以避免不小心透過繼承將成員公開至 COM。 MyProperty SampleClassBy 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 可以看到 managed 類型。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. 屬性不能用來使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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

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

適用於

另請參閱