Skip to content

Instantly share code, notes, and snippets.

@Sov3rain
Created June 1, 2022 13:57
Show Gist options
  • Save Sov3rain/aaa365195c8e2d8852357ae6c4248ff5 to your computer and use it in GitHub Desktop.
Save Sov3rain/aaa365195c8e2d8852357ae6c4248ff5 to your computer and use it in GitHub Desktop.
How to make a deep copy in c# with binary formatter
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
static public class DeepCloning
{
static public T DeepClone<T>(this T source)
{
var formatter = new BinaryFormatter();
using var stream = new MemoryStream();
formatter.Serialize(stream, source);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment