有什么用[标记有ComVisible特性]默认和公共类COM曝光的交易? [英] What's the deal with [ComVisible] default and public classes COM exposure?

查看:432
本文介绍了有什么用[标记有ComVisible特性]默认和公共类COM曝光的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN有这篇文章 [标记有ComVisible特性] 属性。我不太得到,当一个集 [标记有ComVisible特性(真)]



MSDN说


默认为true ,这表明托管类型对COM可见。是不是需要这个属性,使公开托管程序集类型的可见;它们是由默认COM可见。只有公共类型,可取得明显。




所以他们说的公共类型对COM可见默认。但他们也说的只有公共类型可以通过设定可见 [标记有ComVisible特性(真)] 。它不会是有道理的:如果公共类型在默认情况下可见的,那么如何设置 [标记有ComVisible特性(真)] 公开可见的类型?如果他们已经看到他们是如何将得到更明显?



也许我的理解是不正确的。我将不胜感激,如果任何人都可以放一些轻上述声明。


解决方案

它没有品牌某种意义上说,当公共类型在默认情况下可见的,那么,如何标记有ComVisible特性属性设置为true [标记有ComVisible特性(真),使公共类型可见。




他们看到默认情况下,因为ComVisibleAttribute的默认值是true。明确其值设置为true不会改变任何东西,它只是使你的意图更加明确。这是 你找到的文档:




默认为真正,这表明托管类型是可见的COM。是不是需要这个属性使公共管理组件和类型可见;它们是由默认COM可见。只有公开类型,可取得明显。的属性不能被用来使一个原本内部或保护型可见COM或使一个不可见的类型的可见的成员。




基本上,你可以认为它像编译器总是添加 [ComVisibleAttribute(真)] 来你的代码在默认情况下,如果你不自己做。



您需要设置该属性的唯一原因是的阻止的公共类型被COM可见(在这种情况下,你将它设置为false)。默认已经确保了自己的知名度。



显然,非公共类型(例如,private和protected)不能,也永远不会成为对COM可见。该属性对于类型的可访问性等没有影响。


MSDN has this article about [ComVisible] attribute. I don't quite get what happens when one sets [ComVisible(true)].

MSDN says

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.

So they say public types are visible to COM by default. But they also say only public types can be made visible by 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?

Perhaps my understanding is not correct. I shall appreciate if anyone can put some light on the above statements.

解决方案

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.

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 documentation you found:

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. 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.

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.

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). The default already ensures their visibility.

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.

这篇关于有什么用[标记有ComVisible特性]默认和公共类COM曝光的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆