简体   繁体   English

如何使用动态对象类型创建通用List

[英]How to create a generic List with a dynamic object type

I want to create a generic list of the Type object. 我想创建一个Type对象的通用列表。

I have ... 我有 ...

Type type = typeof(Foo);
object model = GetModel();

Now I want to create a new List<Foo>((Foo)model) 现在我想创建一个new List<Foo>((Foo)model)

Is this possible in C#? 这可能在C#中吗?

Type listT = typeof(List<>).MakeGenericType(new[]{type});
object list = Activator.CreateInstance(listT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM