ComVisibleAttribute 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
控制程序集中单独托管类型或成员,或所有类型对 COM 的访问性。
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 的可见性,使其成员不可见。 通过在 Visual Basic 示例中) (SampleClass
上将 设置为 ComVisibleAttribute
MyClass
false
,在 false
和 MyProperty
上MyMethod
,可以避免无意中通过继承向 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
使类型可见。 特性不能用于使 其他或internal
protected
类型对 COM 可见,或使不可见类型的成员可见。
将程序集上的 属性设置为 false
会 public
隐藏程序集中的所有类型。 可以通过将各个类型设置为 ,有选择地使程序集中的类型 true
可见。 在特定类型上将 属性设置为 false
会隐藏该类型及其成员。 但是,如果类型不可见,则无法使类型的成员可见。 在类型上将 属性设置为 false
可防止将该类型导出到类型库;类未注册;接口永远不会响应非托管 QueryInterface
调用。
除非将类及其成员显式设置为 false
,否则继承的类可以向在原始类中不可见的 COM 基类成员公开。 例如,如果将 ClassA 设置为 false
,但不将特性应用于其成员,则类及其成员对 COM 不可见。 但是,如果从 ClassA 派生 ClassB 并将 ClassB 导出到 COM,则 ClassA 成员将成为 ClassB 的可见基类成员。
有关导出过程的详细说明,请参阅 程序集到类型库转换摘要。
Com |
初始化 |
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
Get |
返回此实例的哈希代码。 (继承自 Attribute) |
Get |
获取当前实例的 Type。 (继承自 Object) |
Is |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
To |
返回表示当前对象的字符串。 (继承自 Object) |
_Attribute. |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute. |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute. |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute. |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |