[ComVisible] 默认和公共类 COM 暴露是怎么回事? [英] What's the deal with [ComVisible] default and public classes COM exposure?

查看:12
本文介绍了[ComVisible] 默认和公共类 COM 暴露是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN 有 这篇关于 [ComVisible] 属性.我不太明白设置 [ComVisible(true)] 时会发生什么.

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

MSDN 说

默认为 true,表示托管类型对 COM 可见.公共托管程序集类型可见不需要此属性;默认情况下,它们对 COM 可见.只能使公共类型可见.

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.

所以他们说公共类型默认对 COM 可见.但他们也说只有公共类型可以通过设置[ComVisible(true)] 变为可见.这没有意义:如果公共类型默认可见,那么设置 [ComVisible(true)] 如何使公共类型可见?如果它们已经可见,它们将如何变得更加可见?

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.

推荐答案

没有意义,当公共类型默认可见时,如何将 ComVisible 属性设置为 true [ComVisible(true)] 使公共类型可见.

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.

默认情况下它们是可见的,因为 ComVisibleAttribute 的默认值为 true.将属性显式设置为 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 documentation you found:

默认为true,表示托管类型对COM可见.使公共托管程序集和类型可见,不需要此属性;默认情况下,它们对 COM 可见.只有 public 类型可以显示.该属性不能用于使其他内部或受保护的类型对 COM 可见或使不可见类型的成员可见.

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.

基本上,你可以把它想象成编译器总是默认添加 [ComVisibleAttribute(true)] 到你的代码中,如果你不自己做的话.

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.

您需要设置此属性的唯一原因是防止公共类型对 COM 可见(在这种情况下,您可以将其设置为 false).默认设置已经确保了它们的可见性.

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.

显然,非公共类型(例如私有和受保护)不能也永远不会对 COM 可见.此属性对具有此类可访问性的类型没有影响.

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天全站免登陆