views:

234

answers:

2

I am referencing a COM library in Visual Studio, so it has automatically created the corresponding Interop assembly for me. I would like to do a GetType() on these com objects, but they always return System.__ComObject. Querying them for an interface works though:

bool isOfType = someComeObject is ISomeComObject; //this works

But what I really want is this to return the actual type of the com object:

Type type = someComeObject.GetType(); //returns System.__ComObject :-(

Does anyone know how to do what I want to do?

A: 

You've basically figured it out. GetType() on a COM object is going to give you System.__ComObject, and you have to try to cast it to something else to see what the object really is.

catfood
Hm..is there really no other way? Because I can't test for every possible COM object in existing, I just want the type. The reason is that I am using the type of an object as a key in a dictionary...
Hermann
+3  A: 

Add reference to Microsoft.VisualBasic.dll and then:

Microsoft.VisualBasic.Information.TypeName(someCOMObject)

MSDN reference here.

Darin Dimitrov
I just tried this and it works! It does not return the full name though, just the class name, but that's ok for my purposes. I looked at this method in reflector which internally calls 'LegacyTypeNameOfCOMObject' but I can't figure out what it actually does.
Hermann
I wish there was something that could give me the full name of that com object to avoid clashes.
Hermann
Thanks. Any idea if this is possible in C#?
ragu.pattabi
`Microsoft.VisualBasic.dll` is a .NET assembly which can be referenced and used in every application.
Darin Dimitrov
Watch out if you have COM component written in C++/ATL. You might get a different result than you expect.
ragu.pattabi