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