MSDN 有 this article about [ComVisible]
attribute .我不太明白设置 [ComVisible(true)]
时会发生什么。
MSDN 说
The default is true, which indicates that the managed type is visible to COM. This attribute is not needed to make public managed assemblies and types visible; they are visible to COM by default. Only public types can be made visible.
所以他们说默认情况下公共(public)类型对 COM 可见。但他们也说只有公共(public)类型可以通过设置[ComVisible(true)]
显示。这没有意义:如果默认情况下公共(public)类型可见,那么设置 [ComVisible(true)]
如何使公共(public)类型可见?如果它们已经可见,它们将如何变得更加可见?
也许我的理解不正确。如果有人能阐明上述陈述,我将不胜感激。
最佳答案
技巧是您还可以在程序集级别(在 AssemblyInfo.cs 中)添加此属性。如果您指定 [assembly: ComVisible(true)]
(或者不在程序集级别指定,因此默认情况下具有相同的效果)那么所有公共(public)类和接口(interface)及其公共(public)方法都成为 COM -默认可见。
您也可以在程序集级别设置 [assembly: ComVisible(false)]
,然后默认情况下所有公共(public)实体都将具有与具有 [ComVisible( false)]
在它们上面,所以你只能标记那些你真正需要的类/接口(interface)/方法 COM-visible ([ComVisible(true)]
)。
当您有很多公共(public)实体时,这可以帮助您避免暴露太多,例如 here .如果没有这种机制,您将不得不为您不想公开的每个类/接口(interface)/方法设置 [ComVisible(false)]
。使用 [assembly: ComVisible(false)]
可以让你只暴露你需要的东西。
并且您只能将 public
实体公开给 COM(默认或显式)- 具有更严格可见性的实体不能公开给 COM。
关于c# - [ComVisible] 默认和公共(public)类 COM 公开有什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688395/