ComVisibleAttribute 類別

定義

控制公開至 COM 之個別 Managed 類型或成員,或組件內所有類型的存取範圍。

public ref class ComVisibleAttribute sealed : Attribute
[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
[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)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ComVisibleAttribute : Attribute
[<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)>]
type ComVisibleAttribute = class
    inherit Attribute
[<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)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ComVisibleAttribute = class
    inherit Attribute
Public NotInheritable Class ComVisibleAttribute
Inherits Attribute
繼承
ComVisibleAttribute
屬性

範例

下列範例示範如何控制類別 COM 的可見度,使其成員不可見。 藉由在 ComVisibleAttribute Visual Basic 範例中的 (SampleClass 設定為 false) ,並在 和 falseMyPropertyMyClassMyMethod ,您可以避免不小心透過繼承將成員公開至 COM。

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

備註

您可以將此屬性套用至元件、介面、類別、結構、委派、列舉、欄位、方法、事件存取子或屬性。

預設值為 true ,表示 COM 可以看到 Managed 類型。 不需要這個屬性,即可讓公用 Managed 元件和類型可見;COM 預設會顯示它們。 只能 public 讓類型成為可見的。 屬性無法用來讓 COM 顯示或類型, internalprotected 或讓非可見類型的成員變成可見。

將 元件上的 屬性設定為 falsepublic 隱藏元件中的所有類型。 您可以將個別類型設定為 true ,以選擇性地在元件內顯示型別。 在特定類型上將 屬性設定為 false 會隱藏該類型及其成員。 不過,如果類型不可見,則無法讓類型的成員變成可見。 在類型上將 屬性設定為 false 可防止該類型匯出至型別程式庫;類別未登錄;介面永遠不會回應 Unmanaged QueryInterface 呼叫。

除非您明確將類別及其成員設定為 false ,否則繼承的類別可以公開至原始類別中看不到的 COM 基類成員。 例如,如果您將 ClassA 設定為 false ,且不會將 屬性套用至其成員,則 COM 看不到類別及其成員。 不過,如果您從 ClassA 衍生 ClassB 並將 ClassB 匯出至 COM,ClassA 成員就會變成 ClassB 的可見基類成員。

如需匯出程式的詳細描述,請參閱 元件至型別程式庫轉換摘要

建構函式

ComVisibleAttribute(Boolean)

初始化 ComVisibleAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得值,表示 COM 型別是否為可見。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱