无需传递字符串来识别类的类型。只需使用以下泛型方法调用,它将初始化该 T 类型的列表。
/// <summary>
/// Gets the initialize generic list.
/// </summary>
/// <typeparam name="T"></typeparam>
public IList<T> GetInitializeGenericList<T>() where T : class
{
Type t = typeof(List<>);
Type typeArgs =typeof(T);
Type type = t.MakeGenericType(typeArgs);
// Create the List according to Type T
dynamic reportBlockEntityCollection = Activator.CreateInstance(type);
// If you want to pull the data into initialized list you can fill the data
//dynamic entityObject = Activator.CreateInstance(typeArgs);
//reportBlockEntityCollection.Add(entityObject);
return reportBlockEntityCollection;
}