Created
May 15, 2022 06:18
-
-
Save ajnaduvil/c9f4ffa8bfa10ae39ac0dfcb49218f32 to your computer and use it in GitHub Desktop.
Deep clone using binary serialization
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
| /// <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