日期:2014-05-18  浏览次数:20461 次

反射取值问题,求指点
//当获取的属性的类型是一个类时,就会报出“为将对象引用设置到对象的实例”的异常
下面是伪代码
C# code

   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));
        }
    }



------解决方案--------------------
你现在的代码没问题
------解决方案--------------------
探讨
自己搞定了。