使用反射的通用列表 [英] Generic list by using reflection

查看:70
本文介绍了使用反射的通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方案,我得到类名作为字符串,我只想创建一个通用的 List 该类的。我尝试了以下

I have a scenario on which i got Class name as a string I just want to create a generic List of that class. I tried the following

            Type _type = Type.GetType(className);

            List<_type> list = new List<_type>();

但是iam遇到了错误。我不能将_type作为列表参数。如果有人知道,请帮助我

But iam geting error. I can't gave that _type as list argument. If anybody knows please help me

推荐答案

最近可以得到:

Type listType = typeof(List<>).MakeGenericType(_type);
IList list = (IList) Activator.CreateInstance(listType);

这篇关于使用反射的通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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