使用GetType()</t>创建列表<t>

时间:2010-08-11 00:00:38

标签: c# generics list reflection ienumerable

是否可以使用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>

    }

1 个答案:

答案 0 :(得分:9)

 public static IList GetList(Type type)
 { 
    return (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
 }