tags:

views:

15

answers:

1

I’m studying for Microsoft Certification exams and I came across a situation with a class that was not ComVisible but had methods that were. I assumed that since the class was not visible to COM, that its members would not be accessible. However, apparently the members can be ComVisible even if the class is not.

How can COM access a class member if it cannot access the class in the first place?

I have never had experience with exposing .NET assemblies to COM. This makes little sense to me.

+2  A: 

You probably came across the sample code in the MSDN doc page for ComVisibleAttribute. Yeah, that's bogus. From that same page, emphasis mine:

Setting the attribute to false on a specific type hides that type and its members. However, you cannot make members of a type visible if the type is invisible. Setting the attribute to false on a type prevents that type from being exported to a type library; classes are not registered; interfaces are never responsive to unmanaged QueryInterface calls.

Bad example code.

Hans Passant
+1 and that is reasonable behavior. However it's worth to note that one can mark asssembly not ComVisible adn then he still can mark classes/interfaces ComVisible and expose them to COM separately.
sharptooth