首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

利用反射如何实现这个功能

2012-04-11 
利用反射怎么实现这个功能?我有个类class GeomParas{public GeomParas(){}public double aaa {get set }

利用反射怎么实现这个功能?
我有个类
  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);

热点排行