Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...