简体   繁体   English

与[ComVisible]默认和公共类COM暴露有什么关系?

[英]What's the deal with [ComVisible] default and public classes COM exposure?

MSDN has this article about [ComVisible] attribute . MSDN有关于[ComVisible]属性的这篇文章 I don't quite get what happens when one sets [ComVisible(true)] . 当设置[ComVisible(true)]时,我不太清楚会发生什么。

MSDN says MSDN说

The default is true , which indicates that the managed type is visible to COM. 默认值为true ,表示托管类型对COM可见。 This attribute is not needed to make public managed assemblies and types visible ; 不需要此属性来使公共托管程序集类型可见 ; they are visible to COM by default. 它们在默认情况下对COM可见。 Only public types can be made visible. 只有公共类型才能显示。

So they say public types are visible to COM by default. 所以他们说公共类型默认是COM可见的。 But they also say only public types can be made visible by setting [ComVisible(true)] . 但是他们也说通过设置[ComVisible(true)] 只能使公共类型可见 It does not makes sense: if public types are visible by default, then how does setting [ComVisible(true)] make public types visible? 这没有意义:如果默认情况下公共类型是可见的,那么设置[ComVisible(true)]使公共类型可见? If they're already visible how will they get more visible? 如果它们已经可见,它们将如何变得更加醒目?

Perhaps my understanding is not correct. 也许我的理解不正确。 I shall appreciate if anyone can put some light on the above statements. 如果有人能对上述陈述有所了解,我将不胜感激。

The trick is you can also add this attribute at assembly level (in AssemblyInfo.cs). 诀窍是你也可以在程序集级别添加这个属性(在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. 如果指定[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)]相同的效果,因此您只能标记这些类/ interfaces / methods COM-visible( [ComVisible(true)] )你真正需要的。

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. 如果没有此机制,则必须将[ComVisible(false)]为您不希望公开的每个类/接口/方法。 Using [assembly: ComVisible(false)] lets you only expose the stuff you need. 使用[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公开。

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. 它们默认是可见的,因为ComVisibleAttribute的默认值为true。 Setting the attribute explicitly to true doesn't change anything, it just makes your intentions more clear. 将属性显式设置为true不会改变任何内容,只会使您的意图更加清晰。 That's the very first line of the documentation you found: 这是您找到的文档的第一行:

The default is true , which indicates that the managed type is visible to COM. 默认值为true ,表示托管类型对COM可见。 This attribute is not needed to make public managed assemblies and types visible; 不需要此属性来使公共托管程序集和类型可见; they are visible to COM by default. 它们在默认情况下对COM可见。 Only public types 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. 该属性不能用于使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)]编译器默认情况下将[ComVisibleAttribute(true)]到您的代码中,如果您自己不这样做的话。

The only reason you would need to set this attribute is to prevent public types from being COM-visible (in which case you would set it to false). 您需要设置此属性的唯一原因是为了防止公共类型变为COM可见(在这种情况下,您将其设置为false)。 The default already ensures their visibility. 默认值已确保其可见性。

Obviously, non-public types (eg, private and protected) cannot and will not ever be visible to COM. 显然,非公开类型(例如,私有和受保护的)不能也不会被COM看到。 This attribute has no effect on types with such accessibility. 此属性对具有此类可访问性的类型没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM