反射,比较两个类的属性值的改变
例如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
浙公网安备 33010602011771号