.Net Cloning

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...

Blogged with the Flock Browser
Advertisements

One thought on “.Net Cloning

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s