解决方案 »
- 求救:c#程序报错:运算符 & 无法应用于 long 和 ulong 类型的操作数
- windows服务执行问题
- 请朋友帮我写一个正则表达式。
- 如果输出GB2312码?
- (winform)怎么让Datagridview 的某一单元格处于编辑状态???
- 如何给winform中richtextbox添加背景图片?
- ListBox怎么绑定到Hashtable?
- 怎样获取combox列表的字符串
- 如何把时间转换成9999-12-31T08:00:00+08:00这样的实践格式?
- VB中的inputbox 在C#中用什么代替?
- C#excel数据导入DataGridView第一行数据变成标题处理方法
- 关于 set; get; 的一些问题
获取表示泛型类型的类型实参的 Type 对象的数组,这个只不过是获取泛型中的参数类型,我是想遍历出所有值得啊,这个弄出来了有啥用
获取表示泛型类型的类型实参的 Type 对象的数组,这个只不过是获取泛型中的参数类型,我是想遍历出所有值得啊,这个弄出来了有啥用
对象才有值,类哪有。
获取表示泛型类型的类型实参的 Type 对象的数组,这个只不过是获取泛型中的参数类型,我是想遍历出所有值得啊,这个弄出来了有啥用
对象才有值,类哪有。
就是因为反射后很难获取那个反射的对象,所以才头疼。
class A<T>
{
public T value { get; set; }
}A<int> a = new A<int>();
a.value = 1;
那么
int i = (int)a.GetType().GetProperty("value").GetValue(a, null);一样就可以获得。
你那个是实例化好就知道类型了,我来贴个代码,你帮忙参谋参谋吧
Public Class Test
{
public string Name = “hello”;
public int id = 1 ;
public List<int> list {get;set;}
public Dictionary<int,List<int>> dictionary {get;set}
public Test()
{
list = new List<int>();
list.add(11);
list.add(22);
dictionary = new Dictionary<int,List<int>>();
dictionary.add(1,list);
}
}
现在就是要用反射,获取类Test中的所有属性和其值!
var list = t.GetType().GetProperty("list").GetValue(t, null);
foreach (object o in (list as IEnumerable))
{
Console.WriteLine(o);
}
好神奇的写法,object o in (list as IEnumerable)
可以解释一下吗,如果要搞定那个Dictionary,该怎么办?
好神奇的写法,object o in (list as IEnumerable)
可以解释一下吗,如果要搞定那个Dictionary,该怎么办?
一样的,反射得到Keys Values,然后枚举。
好神奇的写法,object o in (list as IEnumerable)
可以解释一下吗,如果要搞定那个Dictionary,该怎么办?
一样的,反射得到Keys Values,然后枚举。
我的意思是,如果你获取的那个objec o,不是值类型,而是一个引用,那你打出的o就有问题了,如何区分呢?
Test t = new Test();
var list = t.GetType().GetProperty("dictionary").GetValue(t, null);
foreach (object o in (list as IEnumerable))
{
var itemKey = o.GetType().GetProperty("Key").GetValue(o, null);
Console.WriteLine(itemKey); var itemValue = o.GetType().GetProperty("Value").GetValue(o, null);
foreach (object subItem in (itemValue as IEnumerable))
Console.WriteLine(subItem);
}