Created
June 1, 2022 13:57
-
-
Save Sov3rain/aaa365195c8e2d8852357ae6c4248ff5 to your computer and use it in GitHub Desktop.
How to make a deep copy in c# with binary formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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