开发者

Reflection Help - Set properties on object based on another object

开发者 https://www.devze.com 2022-12-18 16:55 出处:网络
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 properti

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

精彩评论

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