.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();
ms.Position = 0;
dest = (T)formatter.Deserialize(ms);
.NET Generic method to clone objects of serializable types (655 Views)