Skip to content

Instantly share code, notes, and snippets.

@loonison123
Created August 23, 2014 23:05
Show Gist options
  • Select an option

  • Save loonison123/5266795a754d23b2d7ba to your computer and use it in GitHub Desktop.

Select an option

Save loonison123/5266795a754d23b2d7ba to your computer and use it in GitHub Desktop.
C# Object Cloning Snippets
// 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