*W*问一个关于反射的问题 我有一个类,里面有一些属性器,我在外面new了一个这个类的实例,给一些属性器赋了值,我现在想取得这些值,不用属性器的方式访问.我要通过反射来取得这些属性器的值或者这些属性器对应的私有字段的值该怎么做呢?属性的名字我已经反射得到了,问题是值去不到各位大虾帮帮忙,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先得到PropertyInfo fi再object temp = fi.GetValue(obj,null);temp 就是你要的 Type type = Type.GetType("ConsoleApplication1.Love");Object obj = type.InvokeMember("Love", BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);//获取字段值 int f = (int)type.InvokeMember("field1", BindingFlags.GetField, null, obj, null); //如果属性是private 的,需要设置Flag才能拿到foreach(PropertyInfo p in o.GetType().GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance)) { object obj = p.GetValue(o,null); Console.WriteLine("{0}={1}", p.Name, obj); } 呵呵,我的文章讲的刚好就是你提的。我写了篇很简单的反射基本应用的文章以及反射在项目中实际使用的文章http://blog.csdn.net/go2newlife/archive/2007/11/27/1904687.aspx 导入和导出的问题 怎么获得textbox中焦点所在的位置。 登录服务器中的会议号是什么? 各位大侠,请问一下如何改变数据库中将性别改成对应的图片 大伙能不能给发邮件发送的类,或者提供下思路,我的yahoo邮箱老是收不到邮件 .net 很简单的问题 高手们帮忙解决,谢谢 用getdate 怎么存入秒,数据库存为2008-6-17 18:31:00 秒为00了 如何判断字段A里边包括字段B?? 超级郁闷,我lp居然找不到什么原因,大家帮我看看 用asp.net方式如何在客户端将某个文件上载到后端ORACLE数据库的BLOB字段中? 关于gridview 显示区域问题 同一页面如何实现多个查询功能
再object temp = fi.GetValue(obj,null);
temp 就是你要的
Object obj = type.InvokeMember("Love",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
//获取字段值
int f = (int)type.InvokeMember("field1", BindingFlags.GetField, null, obj, null);
foreach(PropertyInfo p in o.GetType().GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
{
object obj = p.GetValue(o,null);
Console.WriteLine("{0}={1}", p.Name, obj);
}
我写了篇很简单的反射基本应用的文章以及反射在项目中实际使用的文章
http://blog.csdn.net/go2newlife/archive/2007/11/27/1904687.aspx