Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep cloning objects

Tags:

c#

.net

clone

I want to do something like:

MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); 

And then make changes to the new object that are not reflected in the original object.

I don't often need this functionality, so when it's been necessary, I've resorted to creating a new object and then copying each property individually, but it always leaves me with the feeling that there is a better or more elegant way of handling the situation.

How can I clone or deep copy an object so that the cloned object can be modified without any changes being reflected in the original object?

like image 954
NakedBrunch Avatar asked Sep 17 '08 00:09

NakedBrunch


People also ask

What is deep cloning of object?

A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made.

Is object create deep copy?

Object. assign does not copy prototype properties and methods. This method does not create a deep copy of Source Object, it makes a shallow copy of the data. For the properties containing reference or complex data, the reference is copied to the destination object, instead of creating a separate object.

What is the most efficient way to deep clone an object in JavaScript?

According to the benchmark test, the fastest way to deep clone an object in javascript is to use lodash deep clone function since Object. assign supports only shallow copy.


1 Answers

Whereas one approach is to implement the ICloneable interface (described here, so I won't regurgitate), here's a nice deep clone object copier I found on The Code Project a while ago and incorporated it into our code. As mentioned elsewhere, it requires your objects to be serializable.

using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;  /// <summary> /// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx /// Provides a method for performing a deep copy of an object. /// Binary Serialization is used to perform the copy. /// </summary> public static class ObjectCopier {     /// <summary>     /// Perform a deep copy of the object via serialization.     /// </summary>     /// <typeparam name="T">The type of object being copied.</typeparam>     /// <param name="source">The object instance to copy.</param>     /// <returns>A deep copy of the object.</returns>     public static T Clone<T>(T source)     {         if (!typeof(T).IsSerializable)         {             throw new ArgumentException("The type must be serializable.", nameof(source));         }          // Don't serialize a null object, simply return the default for that object         if (ReferenceEquals(source, null)) return default;          using var Stream stream = new MemoryStream();         IFormatter formatter = new BinaryFormatter();         formatter.Serialize(stream, source);         stream.Seek(0, SeekOrigin.Begin);         return (T)formatter.Deserialize(stream);     } } 

The idea is that it serializes your object and then deserializes it into a fresh object. The benefit is that you don't have to concern yourself about cloning everything when an object gets too complex.

In case of you prefer to use the new extension methods of C# 3.0, change the method to have the following signature:

public static T Clone<T>(this T source) {    // ... } 

Now the method call simply becomes objectBeingCloned.Clone();.

EDIT (January 10 2015) Thought I'd revisit this, to mention I recently started using (Newtonsoft) Json to do this, it should be lighter, and avoids the overhead of [Serializable] tags. (NB @atconway has pointed out in the comments that private members are not cloned using the JSON method)

/// <summary> /// Perform a deep Copy of the object, using Json as a serialization method. NOTE: Private members are not cloned using this method. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T CloneJson<T>(this T source) {                 // Don't serialize a null object, simply return the default for that object     if (ReferenceEquals(source, null)) return default;      // initialize inner objects individually     // for example in default constructor some list property initialized with some values,     // but in 'source' these items are cleaned -     // without ObjectCreationHandling.Replace default constructor values will be added to result     var deserializeSettings = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};      return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings); } 
like image 60
23 revs, 17 users 56% Avatar answered Sep 17 '22 04:09

23 revs, 17 users 56%