2011-03-31 44 views
1
namespace Stuff 
{ 
    class MyStuff 
    { 
      ParmBlock MyParmSet = new ParmBlock(); 

      public void DoThis() 
      { 
      ... 
      ParmBlock Parms = Get_The_Parms(); 
      } 
      Private ParmBlock 
      { 
       Get 
       { 
        Return MyParmSet; 
       } 
      } 

    } 
    Class ParmBlock 
    { 
      private string _Parm1; 
      private string _Parm2; 
      private int _Parm3; 

      public string Parm1 
      { 
       get 
       { 
        return _Parm1;    
       } 
       set 
       { 
        _Parm1 = Value; 
       } 
      } 

      public string Parm2 
      { 
       get 
       { 
        return _Parm2;    
       } 
       set 
       { 
        _Parm2 = Value; 
       } 
      } 

      public int Parm3 
      { 
       get 
       { 
        return _Parm3;    
       } 
       set 
       { 
        _Parm3 = Value; 
       } 
      }  
     } 
} 

我的问题是,我可以使用Activator.Createinstance对mystuff,并且工程很好,但我如何设置ParmBlock中的参数?一切我用尽迄今已经失败和IM慢慢去这里坚果.....使用兄弟类的反射设置属性c#

感谢

回答

2

使用您通过到的getProperty调用获得的PropertyInfo()的类型本身调用。然后你可以使用PropertyInfo.GetValue()和PropertyInfo.SetValue方法。

void example(Object target, string propertyName) 
{ 
    PropertyInfo info = typeof(target).GetProperty(propertyName); 

    object value = info.GetValue(target, new object[] {}); 
} 

心连心

马里奥

+0

或在此还看到了一个更复杂的例子:http://stackoverflow.com/questions/1784446/set-nested-property-values-using-reflections – 2011-03-31 05:32:08