![[clone]](http://www.csharphelper.com/howto_clone_with_serialization.png)
If a class is serializable, then you can create a deep clone of an object from that class by serializing it and the deserializing it. This example uses the following code to define a generic extension method that clones objects from any serializable class.
// Return a deep clone of an object of type T. public static T DeepClone<T>(this T obj) { using (MemoryStream memory_stream = new MemoryStream()) { // Serialize the object into the memory stream. BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(memory_stream, obj); // Rewind the stream and use it to create a new object. memory_stream.Position = 0; return (T)formatter.Deserialize(memory_stream); } }
The code creates a BinaryFormatter and uses it to serialize the object into a memory stream. It then uses the formatter to deserialize the stream into a new object.
Using this extension method is simple. The following code shows how the example program uses it to make a deep clone of the object named person1.
person3 = person1.DeepClone();
For this to work, the class must be marked as serializable. The clone also only includes members of the class that are serializable. For example, if some of the class’s properties are marked with the XmlIgnore attribute, then they won’t be serialized or deserialized so the clone won’t get the original object’s values for those properties.
This example serializes a Person object. The following code shows the Person class’s declaration.
[Serializable()] class Person : ICloneable { ... }
The Person class has an Address property that is of the StreetAddress class. That class is also marked serializable so the DeepClone method can copy its value into the new Person object.




This was interesting, but what I’d like to know now is what’s involved in creating serializable classes?
Also what about attached properties?
And does deserialization use the class’s property setters so any side effects of the setter would be invoked or not? This is probably related to my 1st question about how to make a class serializable.
A future article on these questions would be awesome. Thanks.
The basics are fairly easy. Just add the Serializable attribute (in the System.ComponentModel namespace) to the class.
There are other attribute that can modify how properties are serialized. For example, you can make a property be serialized as an XML attribute instead of as a separate element, or you can make the serialization ignore a property. See these posts for some more details:
I’m not sure how attached properties work with serialization. I haven’t worked with serialization in WPF.
Finally, yes the serializers and deserializers use the class’s property methods rather than setting backing variables directly so any side effects would take place. In particular, deserializers create an object before setting any of its properties so they require that there be a constructor that takes no parameters (so they can initially make the object). This is occasionally a bit annoying but I don’t know of any good way around it.
I hope that helps. Let me know if you have other questions.
Thanks Rod,
Yes that helps. I haven’t followed your links yet, but I may later.
So marking an object Serializable seems like it would be useful only for simpler objects, and implementing ICloneable is probably the way to go for more complex objects where there are private fields which need copying to the clone, and events that maybe should not be fired etc.
ICloneable would be more useful if Microsoft had included a parameter to tell you whether you should make a shallow or deep clone. As it is, your results may vary and you need to be careful not to create mixed results such as a deep clone that includes fields that refer to objects that were cloned shallowly. (If that makes sense.)
In general I would:
Pingback: Clone lists and arrays of objects in C# - C# HelperC# Helper