mardi 7 janvier 2020

Get generic list from object (over reflection)

I query some data via reflection. The returned data type is System.data.datacolumn[] (variable "test"). I would like to convert it into a generic list (e.g. string List). Is that possible?

public IEnumerable<string> GetStringList(string property)
{
   var test =  GetPropertyValue(SomeObject, property);

    // MAGIC //

   return test;
}


public object GetPropertyValue(object obj, string propertyName)
{
    var propertyNames = propertyName.Split('.');

    foreach (var t in propertyNames)
    {
        if (obj != null)
        {
            var propertyInfo = obj.GetType().GetProperty(t);
            obj = propertyInfo != null ? propertyInfo.GetValue(obj) : null;
        }
    }

    return obj;
}




Aucun commentaire:

Enregistrer un commentaire