Cloning Extension Method
As you know cloning creates a replica of the instance of a type. There are basically 2 ways of cloning. Shallow Copywhich means the original object and the copied object reference the same memory location after the process and Deep Copywhich 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.
Thing to note here is that the class which needs to be cloned has to be marked as Serializablefor 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.
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.
Comments