开发者

using reflections with sibling classes to set properties c#

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 Pa开发者_StackOverflow中文版rm2
           {
              get
                 {
                    return _Parm2;            
                 }
              set
                 {
                    _Parm2 = Value;
                 }
           }

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

My problem is that i can use Activator.Createinstance against mystuff and that works great but how do i set the parameters in ParmBlock? everything ive tried so far has failed and im slowly going nuts here.....

thanks


Use the PropertyInfo you get via a call to the GetProperty() call on the type itself. Then you can use the PropertyInfo.GetValue() and PropertyInfo.SetValue Methods.

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

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

hth

Mario

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜