dimanche 8 mars 2020

How to iterate all properties including IEnumerable collections using reflection?

Supposing I have the following classes;

public class Foo
{
    public string Reference { get; set; }
    public CompanyModel Company { get; set; }
    public CallDataModel CallData { get; set; }
}

public class CompanyModel
{
    public string Name { get; set; }
}

public class CallDataModel
{
    public IEnumerable<CallModel> Item { get; set; }
}

public class CallModel
{
    public string Name { get; set; }
    public int Value { get; set; }
}

Using reflection please can someone help me iterate through all properties including the IEnumerable collection.

I would like to be able to map out each propery name and corresponding value within a recursive method.

For example;

public void GetPropertiesRescursively<T>(this T model)
{
    foreach (PropertyInfo prop in model.GetType().GetProperties())
    {
        // Do something
    }
}

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire