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

前端 未结 2 1054
轮回少年
轮回少年 2020-12-29 19:56

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

MSDN says

T

2条回答
  •  生来不讨喜
    2020-12-29 20:40

    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.

    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.

    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.

    And you only can expose public entities to COM (be default or explicitly) - entities with stricter visibility can't be exposed to COM.

提交回复
热议问题