How to deep copy between objects of different types in C#.NET

前端 未结 7 1274
孤街浪徒
孤街浪徒 2020-12-06 01:55

I have a requirement to map all of the field values and child collections between ObjectV1 and ObjectV2 by field name. ObjectV2 is in a different namspace to ObjectV1.

7条回答
  •  孤街浪徒
    2020-12-06 02:39

    As an alternative to using reflection every time, you could create a helper class which dynamically creates copy methods using Reflection.Emit - this would mean you only get the performance hit on startup. This may give you the combination of flexibility and performance that you need.

    As Reflection.Emit is quite clunky, I would suggest checking out this Reflector addin, which is brilliant for building this sort of code.

提交回复
热议问题