Home
Gallery
GuestBook
SourceCode
Contact

Cloning Extension Method

Posted in C#
This post has been read 229 times

As you know cloning creates a replica of the instance of a type. There are basically 2 ways of cloning. Shallow Copy which means the original object and the copied object reference the same memory location after the process and Deep Copy which means the original object and the copied object do not reference the same location after the process. You can shallow copy the object by using the protected MemberwiseClone method of the object. You can implement the Clone method of the ICloneable interface for each class you have to clone for the deep copy. I somehow did not like the idea of implementing this Clone method in each and every class that I wanted to clone, so I created a generic class  to get the deep cloning working. Here is the code.


    1 public class GenericClone

    2 {

    3     public T DeepClone<T>() where T : class

    4     {

    5         object cloned = new object();

    6         BinaryFormatter formatter = new BinaryFormatter();

    7         using (MemoryStream stream = new MemoryStream())

    8         {

    9             formatter.Serialize(stream, this);

   10             stream.Flush();

   11             stream.Position = 0;

   12             cloned = formatter.Deserialize(stream);

   13         }

   14 

   15         return cloned as T;

   16     }

   17 }

   18 

   19 class Test

   20 {

   21     GenericClone getClone = new GenericClone();

   22     public void Clone()

   23     {

   24         Test deeptest = getClone.DeepClone<Test>();

   25     }

   26 }

Thing to note here is that the class which needs to be cloned has to be marked as Serializable for this method to work. To create a clone of a class we just have to create an object of the GenericClone class and call the DeepClone method as in the Test class. The problem here is that we need to have an object of the GenericClone class before we can clone the object. With C# 3.0, now we can write an extension method for the cloning process and call it on the object itself. Here is an example.


    1 public static class ExtensionClone

    2 {

    3     public static T DeepClone<T>(this object obj) where T : class

    4     {

    5         object cloned = new object();

    6         BinaryFormatter formatter = new BinaryFormatter();

    7         using (MemoryStream stream = new MemoryStream())

    8         {

    9             formatter.Serialize(stream, obj);

   10             stream.Flush();

   11             stream.Position = 0;

   12             cloned = formatter.Deserialize(stream);

   13         }

   14 

   15         return cloned as T;

   16     }

   17 }

   18 

   19 class Test

   20 {

   21     public void Get()

   22     {

   23         Test cloned = this.DeepClone<Test>();

   24     }

   25 }

As you can see this is a much cleaner way of cloning the object. I don't have to create an object of the ExtensionClone class to get my cloned object.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets



follow simplyvinay at http://twitter.com

Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll