反射,比较两个类的属性值的改变

例如Student的一个实例stu,stu.GetType()获取当前实例的System.Type,

然后调用方法stu.GetType().GetProperties()返回当前System.Type的所有公共属性,是System.Reflection.PropertyInfo[]的数组,代码:

System.Reflection.PropertyInfo[] stuProperties = stu.GetType().GetProperties();

对数组进行遍历:foreach (PropertyInfo item in stuProperties)

获取属性的名称:string name = item.Name;

获取属性的值:object value = item.GetValue(stu, null);

获取属性的值:

MethodInfo viewModelGET = findItem.GetGetMethod();//viewModel 属性Get访问

object viewModel_value = viewModelGET.Invoke(viewModel, null);

Object PropertyInfo.GetValue(object obj,object[] index)这是用索引化属性的可选索引值返回该属性值,index: 索引化属性的可选索引值,对于非索引化属性,此值为null

posted on 2014-12-04 15:04  二狗你变了  阅读(171)  评论(0)    收藏  举报

导航