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

利用反射怎么实现这个功能?
我有个类
  class GeomParas
  {
  public GeomParas(){}
  public double aaa {get; set; }
  public double bbb { get; set; }
  public double ccc { get; set; }
  .....
  }

我实例一个这个类
PumpGeomParas para =new PumpGeomParas();
假设 用户在对话框上选择了aaa,并设置值为10

------------------------------
怎么用反射,设置para的aaa的值是10;
注:我要用反射而不是用para.aaa = 10;因为我可能每次调用的类不同!

------解决方案--------------------
C# code

            GeomParas p = new GeomParas();
            p.GetType().GetProperty("aaa").SetValue(p, 100f, null);

------解决方案--------------------
GeomParas ob=....;
---------------------------

Type t=typeof(GeomParas);

PropertyInfo pi=t.GetProperty("aaa",BindingFlags.Instance | BindingFlags.Public); 

pi.SetValue(obj,10,null);