日期:2014-05-18 浏览次数:20509 次
public class A { public int Id { get; set; } B model = new B(); public B Model { get { return model; } set { model = value; } } } public class B { public int Id { get; set; } } public void Reflection() { A a = new A(); PropertyInfo[] Arr = a.GetType().GetProperties(); Dictionary<string, object> dic = new Dictionary<string, object>(); foreach (PropertyInfo item in Arr) { //当获取的属性的类型是一个类时,就会报出“为将对象引用设置到对象的实例”的异常 dic.Add(item.Name,item.GetValue(a,null)); } }