-
Notifications
You must be signed in to change notification settings - Fork 7
Home
Have you ever had the need to copy an object? If you had, you should know that the only support from the .NET framework is provided by the MemberwiseClone method. MemberwiseClone performs a bit-by-bit copy for value types, but only copies the reference of reference types.
This kind of copy is known as 'Shallow copy' and it means that every reference field of the object being cloned will share the same instance with the cloned object. As a result, subsequent manipulations of any of the two instances will affect both instances.
If you need a real copy of your object you need a 'Deep copy'. To get a deep copy of an object you have two alternatives:
-
You can get a deep copy of an object via serialization/deserialization. This technique performs bad and involves significant unnecessary overhead.
-
You can use UltraMapper.
If there's only one type involved, things are easy because each member's name and type are always known to match and deep copying is an easier task. If we could handle only this scenario, we could talk about a cloner.
But what if you need to transform your instance of type T into an instance of type V which has different member names and types? UltraMapper can also handle this scenario.
You can configure member mappings manually to inform from where to read and where to write values. By default naming conventions are used to avoid the need to explicitly define member mappings. Common type-to-type conversion are natively supported and you can provide your own.