我需要一些反思的帮助。我将一个对象传递给另一个对象的构造函数。我需要循环遍历参数的属性并基于它设置新对象属性。大多数(但不是全部) params 属性都存在于新对象中。
到目前为止,我已经有了基本的框架。
public DisabilityPaymentAddEntity(DisabilityPaymentPreDisplayEntity preDisplay)
{
Init(preDisplay);
}
private void Init(DisabilityPaymentPreDisplayEntity display)
{
//need some type of loop using reflection here
}
在“Init”方法中,我需要循环“display”的属性,并将任何同名的“DisabilityPaymentAddEntity”属性设置为 preDisplay 中的值。
谁能告诉我我需要做什么?我确信我需要使用 PropertyInfo 等..
谢谢,
~ck 在圣地亚哥
我想是这样的
Type target = typeof(DisabilityPaymentAddEntity);
foreach(PropertyInfo pi in display.GetType().GetProperties())
{
PropertyInfo targetProp = target.GetProperty(pi.Name);
if(targetProp!=null)
{
targetProp.SetValue(this, pi.GetValue(display, null), null);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)