使用兄弟类的反射来设置属性 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 Parm2
{
get
{
return _Parm2;
}
set
{
_Parm2 = Value;
}
}
public int Parm3
{
get
{
return _Parm3;
}
set
{
_Parm3 = Value;
}
}
}
}
我的问题是我可以对 mystuff 使用 Activator.Createinstance ,效果很好,但是我如何在 ParmBlock 中设置参数?到目前为止我尝试的一切都失败了,我在这里慢慢发疯......
谢谢
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;
}
}
}
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用通过对类型本身的 GetProperty() 调用获得的 PropertyInfo。然后您可以使用 PropertyInfo.GetValue() 和 PropertyInfo.SetValue 方法。
马里奥
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.
hth
Mario