mercredi 21 novembre 2018

Get field and property's type of a class recursively

Suppose I have a such class:

public class Class1 {
    public int x1;
    public Class2 x2;
    public double x3;
}

public class Class2 {
    public int y1;
    private int x2;
}

It there any way to get all the type information of all serializable members (I think its all public fields and properties without explicit statement to a private member) of Class1?

For example, I want to create such a type tree:

<Class1>
    <x1>int</x1>
    <x2>Class2</x2>
        <Class2>
            <y1>int</y1>
        </Class2>
    <x3>double</x3>
 </Class1>

The tree will expand unless this member is an atomic type (like int, double, List and all system defined type).

My idea

I know I can use reflection and deep first search to do this thing. But I found out things may become complex because the class can be a generic class. And I'm concerned whether there is more complex situation than generic class.

So there is any better solution than using reflection and deep first search to create this tree?





Aucun commentaire:

Enregistrer un commentaire