创建列表< T>使用GetType() [英] Create List<T> using GetType()
本文介绍了创建列表< T>使用GetType()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用GetType()创建List或IEnumerable.
Is it possible to create a List or IEnumerable using GetType().
// 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>
}
推荐答案
public static IList GetList(Type type)
{
return (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
}
这篇关于创建列表< T>使用GetType()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文