C# [ComVisible] 默认和公共类 COM 暴露有什么关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15688395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the deal with [ComVisible] default and public classes COM exposure?
提问by user2166888
MSDN has this article about [ComVisible]
attribute. I don't quite get what happens when one sets [ComVisible(true)]
.
MSDN 有这篇关于[ComVisible]
属性的文章。我不太明白当一套时会发生什么[ComVisible(true)]
。
MSDN says
MSDN 说
The default is true, which indicates that the managed type is visible to COM. This attribute is not needed to make public managed assembliesand types visible; they are visible to COM by default. Only public types can be made visible.
该默认值为true,这表明托管类型是COM可见。不需要此属性来使公共托管程序集和类型可见;默认情况下,它们对 COM 可见。只能使公共类型可见。
So they say public types are visible to COM by default. But they also say only public types can be made visibleby setting [ComVisible(true)]
. It does not makes sense: if public types are visible by default, then how does setting [ComVisible(true)]
make public types visible? If they're already visible how will they get more visible?
所以他们说默认情况下公共类型对 COM 可见。但他们也说只有公共类型可以通过设置可见[ComVisible(true)]
。没有意义:如果默认情况下公共类型是可见的,那么设置如何[ComVisible(true)]
使公共类型可见?如果它们已经可见,它们将如何变得更加可见?
Perhaps my understanding is not correct. I shall appreciate if anyone can put some light on the above statements.
可能我的理解不正确。如果有人能对上述陈述有所了解,我将不胜感激。
采纳答案by Cody Gray
It does not makes sense, when public types are visible by default, so how does setting ComVisible attribute to true [ComVisible(true)] makes public types visible.
没有意义,当公共类型默认可见时,那么如何将 ComVisible 属性设置为 true [ComVisible(true)] 使公共类型可见。
They're visible by default because the default value of the ComVisibleAttribute is true. Setting the attribute explicitly to true doesn't change anything, it just makes your intentions more clear. That's the very first line of the documentationyou found:
默认情况下它们是可见的,因为 ComVisibleAttribute 的默认值为 true。将属性显式设置为 true 不会改变任何内容,只会让您的意图更加清晰。这是您找到的文档的第一行:
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 publictypes can be made visible. 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.
默认值为true,表示托管类型对 COM 可见。不需要此属性来使公共托管程序集和类型可见;默认情况下,它们对 COM 可见。只能使公共类型可见。该属性不能用于使内部或受保护类型对 COM 可见,或使不可见类型的成员可见。
Basically, you can think of it like the compiler always adds [ComVisibleAttribute(true)]
to your code by default if you don't do it yourself.
基本上,你可以把它想象成[ComVisibleAttribute(true)]
如果你不自己做,编译器总是默认添加到你的代码中。
The only reason you would need to set this attribute is to preventpublic types from being COM-visible (in which case you would set it to false). The default already ensures their visibility.
您需要设置此属性的唯一原因是防止公共类型对 COM 可见(在这种情况下,您可以将其设置为 false)。默认设置已经确保了它们的可见性。
Obviously, non-public types (e.g., private and protected) cannot and will not ever be visible to COM. This attribute has no effect on types with such accessibility.
显然,非公共类型(例如,私有和受保护)不能也永远不会对 COM 可见。此属性对具有此类可访问性的类型没有影响。
回答by sharptooth
The trick is you can also add this attribute at assembly level (in AssemblyInfo.cs). If you specify [assembly: ComVisible(true)]
(or don't specify that at assembly level and so have the same effect by default) then all the public classes and interfaces and public methods thereof become COM-visible by default.
诀窍是您还可以在程序集级别(在 AssemblyInfo.cs 中)添加此属性。如果您指定[assembly: ComVisible(true)]
(或不在程序集级别指定,因此默认情况下具有相同的效果),则所有公共类和接口及其公共方法默认变为 COM 可见。
You could just as well set [assembly: ComVisible(false)]
at assembly level and then all the public entities would by default have the same effect as if they had [ComVisible(false)]
on them and so you could only mark those classes/interfaces/methods COM-visible ([ComVisible(true)]
) which you really need.
您也可以[assembly: ComVisible(false)]
在程序集级别进行设置,然后所有公共实体在默认情况下都具有与它们[ComVisible(false)]
对它们相同的效果,因此您只能标记[ComVisible(true)]
您真正需要的那些类/接口/方法 COM-visible ( )。
This helps you to not expose too much when you have lots of public entities as here. Without this mechanism you would have to set [ComVisible(false)]
to each class/interface/method that you don't want exposed. Using [assembly: ComVisible(false)]
lets you only expose the stuff you need.
这有助于你不要暴露太多,当你有大量的公共实体的位置。如果没有这种机制,您将不得不设置[ComVisible(false)]
您不想公开的每个类/接口/方法。使用[assembly: ComVisible(false)]
让你只暴露你需要的东西。
And you only can expose public
entities to COM (be default or explicitly) - entities with stricter visibility can't be exposed to COM.
并且您只能public
向 COM公开实体(默认或显式) - 具有更严格可见性的实体不能向 COM 公开。