Sunday, October 12, 2008

Reflecting on COM component with ITypeInfo, for when you don’t have Interop assembly/Runtime Callable Wrapper

Lucian's VBlog - Reflection on COM objects

“…

Reflection on .Net objects is done through System.Type and is very easy. For instance, "Dim type = GetType(System.String)" and now you can look at all the members and inheritance hierarchy of the System.String class.

Reflection on COM types is also easy if they have an interop assembly. For instance, add a project reference to the COM Microsoft Speech Library and again do "GetType(SpeechLib.SpVoice)". This lets you reflect on the .Net "Runtime Callable Wrapper" that's in the interop assembly, that was generated from the COM type's type library, and that contains all information that the type library had.

But sometimes you'll be given COM objects that don't have .Net interop assemblies in your code. I ran into this when I wrote a managed plugin for Visual Studio. For reflection here you have to use ITypeInfo instead of System.Type. Here's code to get that ITypeInfo, then dig through it and print out all the members. I'm a novice at COM programming, so I'd welcome suggestions and improvements. (Note: I deliberately didn't attempt to invent some API that would wrap ITypeInfo/TYPEDESC, but it looks ripe for it...)

…”

There’s some interesting code here, code that would probably take me about a billon years (maybe even 700 billion) to discover and figure out myself…

If you are interested in using .Net/VB to peek into the COM world directly from code (i.e. we don’t need to stink’n RCW/Interop…),  you should check out this article.

(via The Visual Basic Team - Reflection on COM objects (Lucian Wischik))

No comments: