通过反射获取对象属性、属性的值,设置对象属性的值


public
class Product { public string ProductCode{get;set;} public string ProductName{get;set;} } //方法,传入对象为Product实例对象 public void GetOrSetValue(object obj) { //获取对象类型 Type t = obj.GetType(); //通过属性名称获取对象属性 PropertyInfo name= t.GetProperty("ProductName"); //获取属性值 object nameValue= name.GetValue(obj, null); //设置属性值 t.GetProperty("ProductCode").SetValue(obj, "88888888", null); }

 

posted @ 2013-07-01 17:23  泉白水  阅读(1130)  评论(0编辑  收藏  举报