有一个对象o1,他有一个abc属性object o1;
string f = "abc"; // o1有abc属性,怎么根据f取出

解决方案 »

  1.   

    Type t = typeof("your class name");propertyInfo[] myproperties=t.GetProperties;
      

  2.   

    Type type = o1.GetType();
    PropertyInfo pi = type.GetProperty(f);
    object obj = pi.GetValue(o1, null);
      

  3.   

    如果abc是private 的,需要设置Flag才能拿到~~~~~
      

  4.   

    using   System.Reflection;  
       
      object   o   =   ...;  
       
      Console.WriteLine(o.GetType().Name);  
       
      foreach   (PropertyInfo   pi   in   o.GetType().GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))  
      {  
            object   v   =   pi.GetValue(o,null);  
            Console.WriteLine("{0}={1}",   pi.Name,   v);  
       
      }