假如有如下一个class,我需要对其所有property进行便利取值,  
  class A
    {
        private int i;
        public int I
        {
            get{return i;}
            set{i = value;}
        }
        private string s;
        public string S
        {
            get{return s;}
            set{s = value;}
        }
    }    foreach (PropertyInfo fi in typeof(A).GetProperties())
    {
        object value = fi.GetValue(structure, null);
    }对string 类型的property进行取值的时候就会出错,这是为什么呢?

解决方案 »

  1.   

    报什么错?string属性反射很正常啊,不可能不行的
      

  2.   

    public static void Test()
    {
        A structure = new A();
        structure.I = 1;
        structure.S = "s";
        foreach (PropertyInfo fi in typeof(A).GetProperties())
        {
            object value = fi.GetValue(structure, null);
            Console.WriteLine(value);
        }
    }
    class A
    {
        private int i;
        public int I
        {
            get { return i; }
            set { i = value; }
        }
        private string s;
        public string S
        {
            get { return s; }
            set { s = value; }
        }
    }输出正确,没问题。
      

  3.   

    A obj= new A();
    ..
    PropertyInfo[] peroperties = typeof(A).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo property in peroperties)
      {
      object value =property.GetValue(obj, null)
      }
      

  4.   

    GetProperties(BindingFlags.Public | BindingFlags.Instance);