Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用兄弟类的反射来设置属性c#_C#_Reflection - Fatal编程技术网

使用兄弟类的反射来设置属性c#

使用兄弟类的反射来设置属性c#,c#,reflection,C#,Reflection,我的问题是,我可以对mystuff使用Activator.Createinstance,这非常有效,但是如何在ParmBlock中设置参数呢?到目前为止,我尝试的一切都失败了,我在这里慢慢发疯 谢谢使用通过对类型本身的GetProperty()调用获得的PropertyInfo。然后可以使用PropertyInfo.GetValue()和PropertyInfo.SetValue方法 namespace Stuff { class MyStuff { Parm

我的问题是,我可以对mystuff使用Activator.Createinstance,这非常有效,但是如何在ParmBlock中设置参数呢?到目前为止,我尝试的一切都失败了,我在这里慢慢发疯


谢谢

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

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;
                 }
           }     
      }  
 }


Mario

或参见此处,了解更详细的示例:
void example( Object target, string propertyName )
{
    PropertyInfo info = typeof(target).GetProperty( propertyName );

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