How would you improve this shallow copying class?

后端 未结 4 1985
暗喜
暗喜 2020-12-19 13:20

I\'ve written a class with a single static method that copies property values from one object to another. It doesn\'t care what type each object is, only that they have ide

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 14:20

    Decide what you want to do if passed objects of types that share some properties but not all. Check for the existence of the property in the From object in the To object before trying to set it's value. Do the "right thing" when you come to a property that doesn't exist. If all of the public properties need to be identical, then you will need to check if you've set all of them on the To object and handle the case where you haven't appropriately.

    I'd also suggest that you may want to use attributes to decorate the properties that need to be copied and ignore others. This would allow you to go back and forth between the two different objects more easily and continue to maintain some public properties that are derived rather than stored on your business object.

提交回复
热议问题