Skip to content

Instantly share code, notes, and snippets.

@pedrovasconcellos
Created June 30, 2021 18:57
Show Gist options
  • Save pedrovasconcellos/87f4bb647f45679e534f891fb144626b to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/87f4bb647f45679e534f891fb144626b to your computer and use it in GitHub Desktop.
Value copy
private static T DeepCopy<T>(T other)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Context = new StreamingContext(StreamingContextStates.Clone);
formatter.Serialize(ms, other);
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