Pages

Sep 11, 2011

Make copy of an object & its properties

Deep copy of object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

///
/// Summary description for CopyObject
///
public class myObject
{
	public myObject()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static object Copy(object objAnyTypeOfObject)
    {
        BinaryFormatter binFormater = new BinaryFormatter();
        MemoryStream memStream = new MemoryStream();
        binFormater.Serialize(memStream, objAnyTypeOfObject);
        //Reset the posotion to initial so that it can read the bytes
        memStream.Position = 0;
        return binFormater.Deserialize(memStream);
    }
}