jeudi 29 mars 2018

Reflection to get List

I'm trying to loop through a DetailClass objects inside a List using reflection just like for string fields, but I can't figure out how.

class DetailClass
{
    public string FieldDetail1 { get; set; }
    public string FieldDetail2 { get; set; }
    public string FieldDetail3 { get; set; }
}

class SomeClass
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
    public List<DetailClass> Artikli { get; set; }
}

private static PropertyInfo[] GetProperties(object obj)
{
    return obj.GetType().GetProperties();
}

var myData = new SomeClass();
var prop = GetProperties(myData);

foreach (var item in prop)
{
    if (item.PropertyType == typeof(string))
    {
        var name = item.Name,  
        var value = item.GetValue(myData).ToString()));
    }

    //how to get name and value for data inside List<DetailClass>?
}





Aucun commentaire:

Enregistrer un commentaire