background preloader

Object Cloning

Facebook Twitter

Copyable: A framework for copying or cloning .NET objects | Circles and Crosses. .NET Deep Copy/Deep Clone between 2 different object types in C# – Some of the Different/Alternative Approaches. I’ve had to use this “Translation” pattern (and similar code) several times over the last few years – so I’ve decided to commit it to a blog entry for future reference. If you have 2 objects (of different types e.g. say 2 different generated WCF client proxies pointing to SAP), with similar generated properties (e.g. both types have the same “Employee” structure with UserId, UserName, Display Name, then you could spend a lot of time trying to write code manually. You could end up writing mapping code like this: employeeDto.EmployeeId = currentEmployee.EmployeeId employeeDto.UserId = currentEmployee.UserId employeeDto.Department = currentEmployee.Department etc.

If you have a simple, flat object that has just value types (e.g. int, strings) with no complex objects (e.g. arrays, generic lists or reference objects) within, then you can simply use a “Shallow Copy” (also known as a “Memberwise Clone”) of the properties between the 2 objects. For example: Object Cloning Using IL in C# Object Deep Cloning using IL in C# - version 1.0. Howdy, If you've read the rest of my blog, then you'll have noticed that I have another post that addresses cloning of objects using IL (Intermediate Language) If you haven't, then you can find the post 'Object cloning using IL in C#'. The reason I published this post is because the code in my previous post (mentioned above) makes a shallow copy of an object.

It copies only references of classes, value-types are off course copied by value. So if you have a class with only value types or strings (immutable), then the shallow copy will act as a deep copy (sort of). Now, imagine that you have (like in most projects) objects that contain other objects, like a person object that has one legal address and different optional addresses. Then you'll probably create a Person class and an Address class, in the person class you'll make a property which has the type 'Address' and a property that's a list of Addresses.

For example: or you can do: The performance of the second option is 'higher'. Object Deep Cloning using IL in C# - version 1.1. C# Object Clone Wars. Cloning C# objects is one of those things that appears easy but is actually quite complicated with many "gotchas. " This article describes the most common ways to clone a C# object. Shallow vs. Deep Cloning There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. Hence, a reference in the original object and the same reference in a shallow-cloned object both point to the same object. ICloneable Interface The ICloneable interface contains a single Clone method, which is used to create a copy of the current object. public interface ICloneable{ object Clone();} The problem with ICloneable is that the Clone method does not explicitly specify whether it is performing a shallow or deep copy, so callers can never be sure. The ICloneable interface contains one member, Clone, which is intended to support cloning beyond that supplied by MemberWiseClone… The MemberwiseClone method creates a shallow copy… Type-Safe Clone 1. 2. 3. 4. 5.