jeudi 7 février 2019

How to add to a list in C# Reflection

As i'm iterating through the properties of a generic object, I may find a List<T>.

So far, i've been able to initialize the list, but I can't seem to get how to get the list type, initialize the object, put data into it, then finally add to the list.

Here's the code, and where i'm stuck:

private static void FillMePlease<T>(T genericThingy)
{
    foreach (PropertyInfo propertyInfo in genericThingy.GetType().GetProperties())
    {
        if (propertyInfo.PropertyType == typeof(string))
        {
            propertyInfo.SetValue(genericThingy, "Test", null);
        }
        else
        {
            //var list = Activator.CreateInstance(propertyInfo.PropertyType);
            //Now I'm stumped.
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire