[英]Reflection Help - Set properties on object based on another object
我可以使用一些幫助。 我將一個對象傳遞給另一個對象的構造函數。 我需要遍歷參數的屬性並基於它設置新的對象屬性。 大多數(但不是全部)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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.