How return the type of a System.__COMObject in System.Type in C#

Jovita picture Jovita · Sep 10, 2014 · Viewed 22.4k times · Source

I'm doing a program and I want to do a Reflection, but for this, I need an Object of the Type class, right? to use the .GetProperties() method... So I tryed this:

Type typeName = simObjects.getType();

But the .GetType() is returning "System.__COMObject". And this is not helpfull. The same happens with .typeof(). I search and found another code, this one:

Type typeName = (Type)Microsoft.VisualBasic.Information.TypeName(simObjects);

But this method return a String, and I need it in System.Type, Can any genious please help me?

Answer

Jovita picture Jovita · Dec 17, 2014

I did not use reflection as I wished, but it is working pretty fine.

foreach(PropertyDescriptor descrip in TypeDescriptor.GetProperties(COMObject))
{
    if(descrip.Name == "Attribute Name")
    {
        foreach(PropertyDescriptor descrip2 in TypeDescriptor.GetProperties(descrip))
        {
           if(descrip2.Name == "sub attribute Name")
           {
           }
        } 
    }
}

This code returns the name of the attributes, for example, imagine that my COMObject has this attributes:

int age;
string name;
Son Phill;

and Son has:

int age;
string name;

In the first loop, the descrip.Name will be "age", "name" and "Phill", and in the second (suppose that the condition returns true for "Son"), "age" and "name".