Created
August 23, 2014 23:05
-
-
Save loonison123/5266795a754d23b2d7ba to your computer and use it in GitHub Desktop.
C# Object Cloning Snippets
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
| // Source - http://social.msdn.microsoft.com/Forums/en-US/a967b44b-c85c-4afd-a499-f6ff604e2139/how-to-cloneduplicate-an-entity?forum=adodotnetentityframework | |
| private static T DataContractSerialization<T>(T obj) { | |
| DataContractSerializer dcSer = new DataContractSerializer(obj.GetType()); | |
| MemoryStream memoryStream = new MemoryStream(); | |
| dcSer.WriteObject(memoryStream, obj); | |
| memoryStream.Position = 0; | |
| T newObject = (T)dcSer.ReadObject(memoryStream); | |
| return newObject; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment