Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.Net and Deep Copy

Some time ago I had to clone objects and .Net's shallow copy proved to be insufficient – it was necessary to use deep copy. No good tools are provided by .Net itself. If required, the object to be cloned must conform to the ICloneable interface and the Clone() method can be defined for the object. As the classes were not very numerous, but relatively bulky and complicated, there was no point in writing a Clone() method for all of them. I needed something else.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

Comments

Joe said:

Hey Gunnar, what about applying generics so you don't have to cast on the returned object?

public T DeepCopy<T>(T obj)

{

   MemoryStream ms = new MemoryStream();

   BinaryFormatter bf = new BinaryFormatter();

   bf.Serialize(ms, obj);

   ms.Seek(0, SeekOrigin.Begin);

   T retval = (T)bf.Deserialize(ms);

   ms.Close();

   return retval;

}

# October 8, 2007 8:15 AM

Jon von Gillern said:

I've been using a generic version of this for quite some time and I think the performance has been quite good.

public static T DeepCopy<T>(T item)

{

   BinaryFormatter formatter = new BinaryFormatter();

   MemoryStream stream = new MemoryStream();

   formatter.Serialize(stream, item);

   stream.Seek(0, SeekOrigin.Begin);

   T result = (T)formatter.Deserialize(stream);

   stream.Close();

   return result;

}

# October 8, 2007 9:25 AM

DigiMortal said:

Hi guys!

Thank you for your comments and examples. Your example are very useful on .Net versions starting from 2.0. The version I given here can also be used on earlier versions of .Net.

# October 8, 2007 11:25 AM

DigiMortal said:

Greg, I cannot take your opinion as flame. I agree with you completely. It's not a good idea to use this methodics on larger data volumes or on more complex object trees.

# October 9, 2007 3:40 PM

The Janitor said:

Noone aware of the using command?

public static T DeepClone<T>(T item)

{

 using (MemoryStream stream = new MemoryStream())

 {

   BinaryFormatter formatter = new BinaryFormatter();

   formatter.Serialize(stream, item);

   stream.Location = 0;

   return (T) formatter.Deserialize(stream);

 }

}

# October 9, 2007 5:31 PM

Annoyed programmer said:

Hey Digimortal,

I hear the sentement about lazy programing but, I wonder since there is no other way for me, what is better. I have been asked by my boss to do a publisher/subscriber patter that also passes data (kind of more publisher/subscriber mixed with medator) but due to multple subscribers reading a single object I have decided to give each subscriber a deep copy to do what they like with, this alows me to not worry about tracking whos where on the data or wether its still needed. so in this case needing to just make a deep copy of an object is usfull with out knowinwhat it is.

Ps teh boss does nopt want teh subscriber or publisher taking any responsibility of notifying me whether or not teh object is needed still or what it is.

# January 23, 2009 11:07 AM

Thamaraiselvan.m said:

I was searching for deep copying a memorystream itself.

How to do that?

# November 30, 2011 7:07 AM