Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

com

comvisible

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.

like image 729
user2166888 Avatar asked Mar 28 '13 17:03

user2166888


People also ask

What does ComVisible mean?

You set the ComVisible property to true, when you need to expose public methods and functions in your assembly to other applications not written in . NET, which can then use the functionality in your assembly using Microsoft's COM, a standardized interface that binds to components at runtime.

What is ComVisible true in c#?

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.

How to expose public members of a class to COM?

Other public members in the class that are not declared in these interfaces will not be visible to COM, but they will be visible to other .NET objects. To expose properties and methods to COM, you must declare them on the class interface and mark them with a DispId attribute, and implement them in the class.

Are public types visible to COM by default?

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.

What are the requirements for a class to be public?

The class must be public. Properties, methods, and events must be public. Properties and methods must be declared on the class interface. Events must be declared in the event interface. Other public members in the class that are not declared in these interfaces will not be visible to COM, but they will be visible to other .NET objects.

How to make a Class Com visible by default?

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.


1 Answers

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.

like image 139
sharptooth Avatar answered Sep 20 '22 12:09

sharptooth