开发者

Reflection Help - Set properties on object based on another object

I could use a bit of relection help. I am passing an object into the constructor of another object. I need to loop through the parameter's 开发者_如何转开发properties and set the new objects properties based on it. Most, but not all, of the params properties exist in the new object.

I have this so far, the basic skeleton.

  public DisabilityPaymentAddEntity(DisabilityPaymentPreDisplayEntity preDisplay)
  {
      Init(preDisplay);
  }

  private void Init(DisabilityPaymentPreDisplayEntity display)
  {
       //need some type of loop using reflection here
  }

In the 'Init' method, I need to loop through 'display's properties and set any of 'DisabilityPaymentAddEntity' properties of the same name to values in the preDisplay.

Can anyone give me a clue what I need to do? I am sure I need to use PropertyInfo etc..

Thanks, ~ck in San Diego


Something like this I think

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜