Home
Manage Your Code
Snippet: DeepCode (C#)
Title: DeepCode Language: C#
Description: Use to deep clone an object Views: 412
Author: Hong Pham Date Added: 9/30/2008
Copy Code  
1public class MyObject  : ICloneable {
2    public object Clone()
3    {
4        return ObjectUtility.CloneObject(this);
5    }
6  
7}
8public static class ObjectUtility {
9	public static object CloneObject(object obj)
10	{
11   	 using (MemoryStream memStream = new MemoryStream())
12   	 {
13   	     BinaryFormatter binaryFormatter = new BinaryFormatter(null, 
14   	          new StreamingContext(StreamingContextStates.Clone));
15   	     binaryFormatter.Serialize(memStream, obj);
16  	      memStream.Seek(0, SeekOrigin.Begin);
17     	   return binaryFormatter.Deserialize(memStream);
18	    }
19}
20}
21