.NET Generic method to clone objects of serializable types
By Indranil Chatterjee
This provides a sample to illustrate a generic method to copy/clone objects of serializable types.
Often we need to create copy of objects. This sample method can work for all types
so long as they are serializable.
public static T Clone<T>(T source)
{
T dest = default(T);
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, source);
ms.Position = 0;
dest = (T)formatter.Deserialize(ms);
}
return dest;
}
.NET Generic method to clone objects of serializable types (969 Views)