ComVisibleAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
控制元件內個別 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
- 繼承
- 屬性
範例
以下範例說明如何控制類別在 COM 上的可見性,使其成員隱形。 將 ComVisibleAttribute 設為 false,MyClass(Visual Basic 範例中為 SampleClass),並將 MyMethod 和 MyProperty 設為 false,可以避免因繼承而無意中暴露成員於 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 是可見的。 此屬性並非讓公開管理的組件與類型可見所必需;它們預設對 COM 可見。 只有 public 類型能顯示。 屬性不能用來讓 COM 看到其他internalprotected或型態,或讓非可見型別的成員可見。
在組裝體上設定屬性為 會 false 隱藏組裝內所有 public 類型。 你可以選擇性地將組件 true中的類型設為 。 將屬性設為 false 特定型態會隱藏該型別及其成員。 然而,若類型是不可見的,則無法讓該類型的成員顯示。 將屬性設為 可 false 防止該型態匯出到型態函式庫;類別不會被註冊;介面對未受管理 QueryInterface 的呼叫永遠不會有回應。
除非你明確將類別及其成員設為 false,否則繼承的類別可能會暴露給 COM 基底類別成員,這些成員在原始類別中是看不見的。 例如,如果你將 ClassA 設為 但 false 不將其屬性套用於其成員,該類別及其成員對 COM 來說是隱形的。 然而,如果你從 ClassA 衍生出 ClassB,並將 ClassB 匯出到 COM,ClassA 成員就會成為 ClassB 的可見基底成員。
建構函式
| 名稱 | Description |
|---|---|
| ComVisibleAttribute(Boolean) |
初始化 |
屬性
| 名稱 | Description |
|---|---|
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
| Value |
會得到一個值,表示 COM 類型是否可見。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _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) |