Click here to Skip to main content
11,928,786 members (50,666 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

Implementing Deep Cloning via Serializing objects

, 22 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
The implementation for a deep clone helper class


I was looking around on The Code Project and saw an article talking about the different implementation styles for deep cloning an object. The article referred to an article using Reflection for Deep Cloning an object, but had no article to reference for using Serialization.

Using the Code

This is a helper class that can be used to perform a deep copy of an object:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;     
/// <span class="code-SummaryComment"><summary></span>
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// <span class="code-SummaryComment"></summary></span>
public static class ObjectCopier
 /// <span class="code-SummaryComment"><summary></span>
 /// Perform a deep Copy of the object.
 /// <span class="code-SummaryComment"></summary></span>
 /// <span class="code-SummaryComment"><typeparam name="T">The type of object being copied.</typeparam></span>
 /// <span class="code-SummaryComment"><param name="source">The object instance to copy.</param></span>
 /// <span class="code-SummaryComment"><returns>The copied object.</returns></span>
 public static T Clone<T>(T source)
  if (!typeof(T).IsSerializable)
    throw new ArgumentException("The type must be serializable.", "source");
  // Don't serialize a null object, simply return the default for that object
  if (Object.ReferenceEquals(source, null))
    return default(T);

  IFormatter formatter = new BinaryFormatter();
  Stream stream = new MemoryStream();
  using (stream)
    formatter.Serialize(stream, source);
    stream.Seek(0, SeekOrigin.Begin);
    return (T)formatter.Deserialize(stream);

Usage of this class becomes ObjectCopier.Clone(objectBeingCloned);.

Points of Interest

In case 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();.


  • 22nd February, 2008: Version 1.0: Initial release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Stephen Inglish
Software Developer (Senior) Harland Financial Solutions
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhy are you dependant of IsSerializable ? Pin
copa01718-Feb-15 23:49
membercopa01718-Feb-15 23:49 
QuestionNull object clone exception Pin
Thierry Parent14-Sep-14 1:26
memberThierry Parent14-Sep-14 1:26 
QuestionSerialization is very slow, use direct assigning. Pin
comaric31-Jan-13 4:31
membercomaric31-Jan-13 4:31 
AnswerRe: Serialization is very slow, use direct assigning. Pin
reggaeguitar10-Sep-14 8:28
memberreggaeguitar10-Sep-14 8:28 
QuestionHow do I make a Type serializable Pin
AlexanderBlade10-Apr-12 13:08
memberAlexanderBlade10-Apr-12 13:08 
AnswerRe: How do I make a Type serializable Pin
Stephen Inglish10-Apr-12 13:25
memberStephen Inglish10-Apr-12 13:25 
GeneralRe: How do I make a Type serializable Pin
AlexanderBlade11-Apr-12 17:17
memberAlexanderBlade11-Apr-12 17:17 
QuestionAlternative method to copy object to reduce performance penalty Pin
jozamm5-Aug-11 19:32
memberjozamm5-Aug-11 19:32 
AnswerRe: Alternative method to copy object to reduce performance penalty Pin
Stephen Inglish6-Aug-11 5:23
memberStephen Inglish6-Aug-11 5:23 
GeneralFYI Pin
lagerdalek23-May-10 14:04
memberlagerdalek23-May-10 14:04 
GeneralJust One Small Correction... Pin
no-paper20-Mar-09 7:17
memberno-paper20-Mar-09 7:17 
GeneralGood use of generics but be aware Pin
ilya lozovyy29-Apr-08 10:33
memberilya lozovyy29-Apr-08 10:33 
GeneralE=MC^2 Pin
Tom Hawkins17-Apr-08 11:45
memberTom Hawkins17-Apr-08 11:45 
GeneralExcellent idea!!! Pin
Weslei Gomes24-Mar-08 12:15
memberWeslei Gomes24-Mar-08 12:15 
QuestionWhat about the performance penalty? Pin
AndyHo26-Feb-08 3:14
memberAndyHo26-Feb-08 3:14 
GeneralVery Interesting Idea Pin
merlin98125-Feb-08 5:27
membermerlin98125-Feb-08 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid