直接代码
private List<T> CreateListInstance<T>(int Length)
{
List<T> tList = new List<T>();
for (int i = 0; i < Length; i++)
{
tList.Add(Activator.CreateInstance<T>());
}
return tList;
}
private void Initialize()
{
foreach (System.Reflection.PropertyInfo item in this.GetType().GetProperties())
{
if (item.PropertyType == typeof(List<CustomClass1>))
{
item.SetValue(this, CreateListInstance<CustomClass>(5),null);
}
else if(item.PropertyType==typeof(List<CustomClass2>))
{
item.SetValue(this, CreateListInstance<CustomClass2>(), null);
}
}
}