vendredi 14 septembre 2018

Add items to a string list in object by propertyname using reflection

I'm trying to make my code more dynamic and trying to populate string lists by using their propertynames. But I can not figure out how to do that exactly

my code:

Myobject testobject = new Myobject();
PropertyInfo[] properties = testobject.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
    if (property.Name == "Testlist")
    {
        //add someting to the list
    }
}


 //output:
 String test = testobject.Testlist[0]


public class Myobject {
     public List<String> Testlist;
     public List<String> Testlist2;
     public List<String> Testlist3;

     public Myobject()
     {
         Testlist = new List<string>();
         Testlist2 = new List<string>();
         Testlist3 = new List<string>();
     }
}

is it possible to fill lists like that?

Thanks in advance





Aucun commentaire:

Enregistrer un commentaire