How to use reflection on a COM object?

5

I'm trying to do a reflection in a program, however, at the time of picking the object type (which is needed for reflection) is returning "System .__ COMObject", and this is not useful. The method I tried was the following:

Type tipo = simObjects.GetType();

The same return happens when I try with the typeof () method; I researched and found the following method:

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

It returns the right type, however in string format, (it's error in the above code "Can not implicit convert" String "to" System.Type ", but I changed Type by String and it returned the name of the type) can some genius tell me how I can return in Type?

    
asked by anonymous 10.09.2014 / 20:18

2 answers

2

It might not have been so clear what I wanted, but I managed to sort it out by forcing reflection and it turned out there was no need to pick up the type.

foreach(PropertyDescriptor descricao in TypeDescriptor.GetProperties(ObjetoCOM))
{
    if(descricao.Name == "Nome do atributo")
    {
        foreach(PropertyDescriptor descricao2 in TypeDescriptor.GetProperties(descricao))
        {
           if(descricao.Name == "Nome do atributo")
           {
           }
        } 
    }
}

This code returns attribute names, for example, if myCOM object has the following attributes:

int idade;
string nome;
Filho filho;

and child has:

int idade;
string nome;

In the first loop, the description will be "age", "name" and "child", and in the second (assuming the condition is true for the child), "age" and " / p>     

16.12.2014 / 13:55
4

Not possible.

System.__COMObject is a class declared as internal and all its methods are exposed by System.Runtime.InteropServices.Marshal . Therefore, this class is nothing more than a wrapper for the COM object, not having a derived .Net type that can be obtained by the Object.GetType() method.

If you want to see more about this class, I recommend you take a look at the source code:

Editing:

According to your comment, what you want then is to get RTTI from the COM library. C # does not allow this kind of direct interaction with RTTI of non-manageable objects, so what to do?

1. What language your COM library was written to. It is usually C ++, but it can be written in Object Pascal too, for example.

2. Is the COM library compiled with RTTI? If not, there is nothing that can be done other than crying for the library developers to compile it with RTTI. Because? Because without RTTI, there is simply no way to do reflection on an object.

3. Build a library in the same language as your COM library that does the reflection for you. Using P / Invoke you interact with it within C #. Note that even then you will never have an object of type System.Type , because this object only exists in manageable code, which is not the case with your COM library.

Maybe just to build a structure that encapsulates the std::type_info returned by the typeid operator (if it is the C ++ language of the COM library, of course), and build the methods to get this value from its COM class already enough .

The RTTI analysis library is not complicated (or does not seem to me at first), and needs only a few methods implemented, according to your need. Compared to the other solutions I've presented in the comments, it's a fraction of the complexity / effort, no doubt.

    
10.09.2014 / 20:44