Assuming you know the property name, and you know it is an IEnumerable<T>
then this function will set it to a list of corresponding 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);
}
Please note this method does no type checking, or error handling. I leave that as an exercise to the user.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…