如何创建一个动态的对象类型泛型列表 [英] How to create a generic List with a dynamic object type

查看:559
本文介绍了如何创建一个动态的对象类型泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建的对象的类型泛型列表。



我有...

 类型类型= ty​​peof运算(富); 
对象模型= GetModel();

现在我想创建一个新的List<富>((美孚)模型)



在C#这是可能的吗?


解决方案

 键入listT = typeof运算(名单<>)MakeGenericType(新[] {}型)。 
对象列表= Activator.CreateInstance(listT);


I want to create a generic list of the Type object.

I have ...

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

Now I want to create a new List<Foo>((Foo)model)

Is this possible in C#?

解决方案

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

这篇关于如何创建一个动态的对象类型泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆