首页 新闻 赞助 找找看

如果动态生成List<T>

0
悬赏园豆:50 [已解决问题] 解决于 2011-02-25 11:00

如何动态生成List<T>, 现在已知 T 的字符串格式的名称,T 应为另外程序集中的类型。

网上找到一段代码

 

1 string strType = "TourTMP_Quote";
2 Type t = Type.GetType(string.Format("System.Collections.Generic.List`1[{0}]", strType));
3 MethodInfo add = t.GetMethod("Add", new Type[1] { typeof(int) });
4 object list = t.InvokeMember(null,
5 BindingFlags.DeclaredOnly |
6 BindingFlags.Public | BindingFlags.NonPublic |
7 BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { });

 

但是T只能是在相同程序集中的类型,如果T在另外的程序集中,则为 type t 为null。如果要达到我的目的,我该怎么做,麻烦高手们给予指教。

最好能附上代码,谢谢

问题补充: 我现在知道的是 T 的名称,(这种形式:string strType =“TourTMP_Quote”),。 TourTMP_Quote 类型在另外个程序集中,我想实现的是动态生成List<TourTMP_Quote> ,
skyzsx的主页 skyzsx | 初学一级 | 园豆:35
提问于:2011-02-25 10:00
< >
分享
最佳答案
0

typeof(List<>).MakeGenericType(typeof(string)),这样就得到了List<string>的Type。

收获园豆:50
Jeffrey Zhao | 小虾三级 |园豆:1629 | 2011-02-25 10:20
我可能没说清楚 ,我现在知道的是 T 的名称,(这种形式:string strType =“TourTMP_Quote”),。 TourTMP_Quote 类型在另外个程序集中,我想实现的是动态生成List<TourTMP_Quote> ,
skyzsx | 园豆:35 (初学一级) | 2011-02-25 10:41
我明白你的意思了,谢谢!结贴
skyzsx | 园豆:35 (初学一级) | 2011-02-25 10:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册