Dan*_*Dan 3 c# generics reflection ienumerable list
是否可以使用GetType()创建List或IEnumerable.
// If T is Type of Contact I want to Return List<Contact>
Test(typeof(Contact));//return List<Type>
public static IEnumerable<T> Test<T>(T t)
{
return new List<T>(); //return List<Type>
}
Run Code Online (Sandbox Code Playgroud)
public static IList GetList(Type type)
{
return (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
}
Run Code Online (Sandbox Code Playgroud)