Assuming you know the name of the property, and know what it is IEnumerable<T>, then this function will set it to a list of the appropriate type:
public void AssignListProperty(Object obj, String propName)
{
var prop = obj.GetType().GetProperty(propName);
var listType = typeof(List<>);
var genericArgs = prop.PropertyType.GetGenericArguments();
var concreteType = listType.MakeGenericType(genericArgs);
var newList = Activator.CreateInstance(concreteType);
prop.SetValue(obj, newList);
}
Note that this method does not check for type or error handling. I leave this as an exercise for the user.
source
share