.Net Cloning
April 13, 2009 1 Comment
During a project, i implemented LINQ and recursions for converting in memory object to XML (instead of XML or DataContract serialization). I also used recursions, LINQ and reflection for deep copying objects. Here is a prescribed way of cloning using Iclonebale, without LINQ.
Implementing ICloneble for Deep cloning
[Serializable]
public class Customer : ICloneable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
#region ICloneable Members
public object Clone()
{
object clone;
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
// Serialize this object
formatter.Serialize(stream, this);
stream.Position = 0;
// Deserialize to another object
clone = formatter.Deserialize(stream);
}
return clone;
}
#endregion
}
I will be posting on LINQ Clones in my next blog...




Pingback: 2010 in review « Subodh Pushpak's Blog