#144 – Using Serialization to Implement Deep Copying

One alternative to using the ICloneable interface to support making a deep copy for a custom type is to use serialization.

This method will work only if the class to be copied, as well as all classes referenced by it (directly or indirectly), are serializable.

For example, if we have a Person class that has some value-typed members, but also a reference to an instance of the Address class, both the Person and Address classes have to be serializable in order to use this technique.

Here’s a generic method that makes a deep copy using serialization, serializing the object into a stream and then deserializing into a new object.

        public static T DeepCopy<T>(T obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, obj);
                stream.Position = 0;

                return (T)formatter.Deserialize(stream);
            }
        }

Using this method to deep copy a Person:

            Person bronteClone = DeepCopy<Person>(emily);
Advertisement