Thursday, November 22, 2007

Make a copy of the object

if we have a object and want to create a deep copy of it,then we need to implement deepCopy or copy type of method in the class (Type). and this must be repeated with each type (class) you have in ur system and want to create a copy of the object,


this simple class will replicate the object without using much of the resource.

--Feedback are wellcome

thanks
Pradeep


///
/// Public class containing function to replicate object without implementing copy or DeepCopy type of function
///

public class DeepCopyClass
{
///
/// Make a copy of the object passed as argument
///

///
///
public static object DeepCopyOfAnyTypeOfObject(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);
}
}