Here’s a quick way to get a clone of your serializable object in C#:
public static object GetClone(object cloneThis)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms= new MemoryStream();
bf.Serialize(ms, cloneThis);
ms.Flush();
ms.Position = 0;
return bf.Deserialize(ms);
}
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms= new MemoryStream();
bf.Serialize(ms, cloneThis);
ms.Flush();
ms.Position = 0;
return bf.Deserialize(ms);
}
Nice trick to Clone… this reduced my manual clone method from 100s of lines to 6..
Gud one
Nice. I suggest adding a check to the method:
if( !cloneThis.GetType().IsSerializable )
{
throw new ArgumentException( “Object must be serializable.”);
}
Great idea – thanks galaktor
I really liked your method. thanks for sharing this:) hope many people will find it useful as I did. have read lots of articles on the topic, but have never thought that could be so easy
Is it deep clone?