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?
Add reference to
Microsoft.VisualBasic.dll
and then:MSDN reference here.
I stumbled upon this question a few days ago while I was looking for the full type name of a
System.__ComObject
object. I ended up getting the type name using Darin's solution and then looping through all classes in all assemblies to test the match:Not the fastest and most elegant solution, but it worked.
The accepted answer by Darin requires a dependency to
Microsoft.VisualBasic.dll
. If you don't want to have that you can use this simple helper class: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.