mardi 17 novembre 2020

How do I get a list of all properties including those within nested objects within a class file

I have a class with nested properties, similar to the following:

Class X { 
public ClassA propA {get;set;} 
public IEnumerable<ClassB> propB {get;set;}
}

where

Class A { 
    public IEnumberable<ClassC> prop C {get;set;} 
    public ClassD propD{get;set;} // Enum type
}

Class B { 
    public ClassE propE {get;set;} 
    public string prop1...prop10 {get;set;} 
    public IEnumerable<ClassF> propF{get;set;}
...} and so on

I am required to develop a generic method which can accept any Class File and list out all properties along with their values across all these layers. There can be files with a complexity as shared in this example or more!

I have used Reflection and LINQ queries to figure the names of these properties but can only get this information for upto 2 layers and have not been able to identify properties which could be of another class type and obtain it's corresponding properties as well. I would want to keep extensive looping/ recursion as the last approach but would rather prefer LINQ solutions as irrespective of the complexity, this would have to be processed in under a minute.

I seem to have exhausted all options and would love to hear your solutions to this.

Many Thanks!





Aucun commentaire:

Enregistrer un commentaire