mardi 19 juillet 2016

Recursive reflection on All properties of an object in C# How?

I know about reflection in C# but I have a question

Is there any way for finding all properties of an object recursively or not ?

for example see this anonymous object :

public class Person
{
    public string Name { get; set; }
    public string Family { get; set; }

    public List<Address> Addresses { get; set; }

}
public class Address
{
    public string Country { get; set; }
    public string City { get; set; }

    public string[] Phones { get; set; }

}
        // THE OBJECT
        var obj = new
        {
            Name = "Test",
            Status = new[] {
                "S1",
                "S2",
                "S3",
                "S4"
        },
            People = new List<Person> {
    new Person {
        Name = "A",
        Family = "AA",
        Addresses = new List < Address > () {
            new Address {
                City = "A1",
                Country = "A2",
                Phones = new[] {
                    "A3",
                    "A4",
                    "A5"
                }
            }

        }
    },
    new Person {
        Name = "B",
        Family = "BB",
        Addresses = new List < Address > () {
            new Address {
                City = "B1",
                Country = "B2",
                Phones = new[] {
                    "B3",
                    "B4",
                    "B5"
                }
            }

        }
    },
    new Person {
        Name = "C",
        Family = "CC",
        Addresses = new List < Address > () {
            new Address {
                City = "C1",
                Country = "C2",
                Phones = new[] {
                    "C3",
                    "C4",
                    "C5"
                }
            },
            new Address {
                City = "C6",
                Country = "C7",
                Phones = new[] {
                    "C8",
                    "C9",
                    "C10"
                }
            }

        }
    }
}
        };

so you can see this object have some list, so I want to have a dictionary based on tree level of object

        var info = new Dictionary<string, string>()
        {
        //{"PropertyName.LevelNumber.PropertyName.LevelNumber. ...","PropertyValue"}
              { "Name", "Test" },
              { "Status.0", "S1" },
              { "Status.1", "S2" },
              { "Status.2", "S3" },
              { "Status.3", "S4" },
              { "People.0.Name", "A" },
              { "People.0.Family", "AA" },
              { "People.0.Addresses.0.City", "A1" },
              { "People.0.Addresses.0.Country", "A2" },
              { "People.0.Addresses.0.Phones.0", "A3" },
              { "People.0.Addresses.0.Phones.1", "A4" },
              { "People.0.Addresses.0.Phones.2", "A5" },
              { "People.1.Name", "B" },
              { "People.1.Family", "BB" },
              { "People.1.Addresses.0.City", "B1" },
              { "People.1.Addresses.0.Country", "B2" },
              { "People.1.Addresses.0.Phones.0", "B3" },
              { "People.1.Addresses.0.Phones.1", "B4" },
              { "People.1.Addresses.0.Phones.2", "B5" },
              { "People.2.Name", "C" },
              { "People.2.Family", "CC" },
              { "People.2.Addresses.0.City", "C1" },
              { "People.2.Addresses.0.Country", "C2" },
              { "People.2.Addresses.0.Phones.0", "C3" },
              { "People.2.Addresses.0.Phones.1", "C4" },
              { "People.2.Addresses.0.Phones.2", "C5" },
              { "People.2.Addresses.1.City", "C6" },
              { "People.2.Addresses.1.Country", "C7" },
              { "People.2.Addresses.1.Phones.0", "C8" },
              { "People.2.Addresses.1.Phones.1", "C9" },
              { "People.2.Addresses.1.Phones.2", "C10" }
        };

Can someone help me about this recursive reflection and tree level key of dictionay with corresponding value ?





Aucun commentaire:

Enregistrer un commentaire