求教各位大侠。
反射遍历一个class,
已知class的一个成员是IList的,但是不知道具体是哪个泛型参数类型,
也就是<T>是未知的。
现在想要把他的值给取出来,或者能够强制转换object也可以。
这个该怎么做,忘各位大侠热心告知小弟一番。
反射遍历一个class,
已知class的一个成员是IList的,但是不知道具体是哪个泛型参数类型,
也就是<T>是未知的。
现在想要把他的值给取出来,或者能够强制转换object也可以。
这个该怎么做,忘各位大侠热心告知小弟一番。
foreach(object obj in lis)
{
obj...
}
和IS运算符不知道能解决不.
没用过.
这个类里面有个list。其中一部分代码:
else if (info.PropertyType.Name.StartsWith("IList"))
{
object lis = info.GetValue(b, null);
//TODO 这个lis取值要怎么取
}
if(type=="你的类类型")
{
(类) newclass=(类)msgobject;
}
newclass.列表名
string type_full_name = test.GetType().GetGenericArguments()[0].FullName;//System.Int32类型
反射取出list<T>的System.Int32类型后,要怎么去list的值呢?
能告知一下么?,谢谢
A.Add(1);
System.Collections.IEnumerator T = A.GetEnumerator();
while(T.MoveNext())
{
Console.WriteLine(T.Current);
}
test.Add(50);
string type_full_name = test.GetType().GetGenericArguments()[0].FullName;//System.Int32类型
Console.WriteLine(test.GetType().GetMethod("get_Item").Invoke(test, new object[] { 0 }));//取数据
特别感谢 wuyazhe 和 cdglynn 你们的正确指导。
前面一直卡在list的item怎么取上,大多数人都通过list[n]来取。
并且msdn上标明的list方法列表里面,也没有说明有什么具体的方法来去item。
因此不知道怎么反射去item。最后我采用了 wuyazhe 的方法。主要是代码:
test.GetType().GetProperty("Count").GetValue(test, null);//取list 的item的个数。
test.GetType().GetMethod("get_Item").Invoke(test, new object[] { 0 });//取item