我想做类似下面的代码.
public IList SomeMethod(Type t)
{
List<t> list = new List<t>;
return list;
}
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用.是否有其他方法可以使用对Type实例的引用动态设置泛型类的类型参数?
小智 7
试试这个:
public IList SomeMethod(Type t)
{
Type listType = typeof(List<>);
listType = listType.MakeGenericType(new Type[] { t});
return (IList)Activator.CreateInstance(listType);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3213 次 |
| 最近记录: |