C# 反射如何取自定义类型的List<>列表的 Type 类型

spshfh 2010-11-01 02:04:50
代码如下:

Assembly asmPlugIn = Assembly.LoadFrom(strPlugInPath);

string strtype = "userInfo";

//这里可以取到自定义类型(自己声明的类userInfo)的Type
Type itemType = asmPlugIn.GetType(strType);


//用这种方法取到的值为 null (这里要取的是list<userInfo> 列表)
Type valueType = asmPlugIn.GetType(string.Format("System.Collections.Generic.List`1[[{0},{1}]]"
, itemType.Name, asmPlugIn.FullName));
//用这种方法取到的值也为 null
Type valueType2 = Type.GetType(string.Format("System.Collections.Generic.List`1[[{0},{1}]]"
, itemType.Name, asmPlugIn.FullName));
...全文
3891 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
倾蝶 2013-04-30
  • 打赏
  • 举报
回复
genericList.GetType().InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, genericList, new object[] { o1,o2,o3 }); 这样就添加进去啦
倾蝶 2013-04-30
  • 打赏
  • 举报
回复
但是怎么往genericList 里添加EC类型的值呢, 你这样创建的genericList 是一个count等于0的泛型变量, 怎么把ec类型的值添加进去啊
loveic 2012-10-23
  • 打赏
  • 举报
回复
bloodish 太强了!
我也正好用到,满世界的搜索,终于找到了!
liweibird 2011-09-13
  • 打赏
  • 举报
回复
bloodish

你也太强了吧,俺正好用到~~
spshfh 2010-11-01
  • 打赏
  • 举报
回复
多谢大家帮助
特别感谢bloodish
问题解决
bloodish 2010-11-01
  • 打赏
  • 举报
回复

public static object CreateGeneric(Type generic, Type innerType, params object[] args)
{
Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
return Activator.CreateInstance(specificType, args);
}
object genericList = CreateGeneric(typeof(List<>), typeof(EC));
spshfh 2010-11-01
  • 打赏
  • 举报
回复
在我调用的方法里接收一个List<>类型的参数,里面是自定义的EC类, 我要通过反射构建这List对象
spshfh 2010-11-01
  • 打赏
  • 举报
回复
是这样的,我自定义的类在我编译好的dll文件里,我是通过加载这个dll文件,通过反射调用这个dll文件里的方法。
mohugomohu 2010-11-01
  • 打赏
  • 举报
回复
是这样的,反射是运行时获取或生成对象,方法和属性,如果你有了你的自定义的对象名,然后想通过字符串在运行时获取对象,那就用反射,否则没有什么意义。
bloodish 2010-11-01
  • 打赏
  • 举报
回复
获取List<T>中T的类型

public class MyClass
{
}
Type t = typeof(List<MyClass>).GetGenericArguments()[0];
Console.WriteLine(t.Name);
//输出
//MyClass
spshfh 2010-11-01
  • 打赏
  • 举报
回复
itemType.Name 改为 itemType.FullName
也不行,还是取不到
机器人 2010-11-01
  • 打赏
  • 举报
回复
Type valueType2 = Type.GetType(string.Format("System.Collections.Generic.List`1[[{0},{1}]]", itemType.Name, asmPlugIn.FullName));

没有加命名空间吧。

itemType.Name 改为 itemType.FullName
spshfh 2010-11-01
  • 打赏
  • 举报
回复
再补充一点 如果这样写
Type valueType = Type.GetType("System.Collections.Generic.List`1[System.string]");
也是可以取到Type类型的,只是一换成我自己定义的类就不行啦
spshfh 2010-11-01
  • 打赏
  • 举报
回复
Assembly asmPlugIn = Assembly.LoadFrom(strPlugInPath);
//这里可以取到自定义类型(自己声明的类)的Type
Type itemType = asmPlugIn.GetType(strType);


//用这种方法取到的值为 null (这里要取的是list<自定义类> 列表)
Type valueType = asmPlugIn.GetType(string.Format("System.Collections.Generic.List`1[[{0},{1}]]"
, itemType.Name, asmPlugIn.FullName));
//用这种方法取到的值也为 null
Type valueType2 = Type.GetType(string.Format("System.Collections.Generic.List`1[[{0},{1}]]"
, itemType.Name, asmPlugIn.FullName));

我要取到这个类型的list<自定义类> 的Type 去构建这个类型的对象
但是在这里我只知道这它的字符串形式的 Type 类似“System.Collections.Generic.List`1[Reloadsoft.Lolife.PlugIn.Integral.ECL._IntegralEC]”
我是要通过这个字符串取到Type类型
再用调用 Activator.CreateInstance(valueType2 )去去构建这个类型的对象
但在这里我取到的Type类型都为 null 没法进行下面的操作

cdglynn 2010-11-01
  • 打赏
  • 举报
回复
没看懂楼主想做什么
xuld 2010-11-01
  • 打赏
  • 举报
回复
typeof(List<>)
机器人 2010-11-01
  • 打赏
  • 举报
回复
都知道是 List<UserInfo> 了,还反射什么呢?

typeof(List<UserInfo>) 不行吗?

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧