Skip to content

Instantly share code, notes, and snippets.

@ajnaduvil
Created May 15, 2022 06:18
Show Gist options
  • Select an option

  • Save ajnaduvil/c9f4ffa8bfa10ae39ac0dfcb49218f32 to your computer and use it in GitHub Desktop.

Select an option

Save ajnaduvil/c9f4ffa8bfa10ae39ac0dfcb49218f32 to your computer and use it in GitHub Desktop.
Deep clone using binary serialization
/// <summary>
/// A faster implementation of deepclone using binary formatter
/// </summary>
/// <param name="obj">Object to be cloned</param>
/// <returns></returns>
public static T DeepClone<T>(this T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment