Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iskernel/6008480 to your computer and use it in GitHub Desktop.
Save iskernel/6008480 to your computer and use it in GitHub Desktop.
Generic deep copy class in C#
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace IsKernel.Pool.Vanilla.Memory
{
public static class GenericCopyMaker<T>
{
public static T GetDeepCopy(object objectToCopy)
{
using (MemoryStream memoryStream = new MemoryStream())
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, objectToCopy);
memoryStream.Seek(0, SeekOrigin.Begin);
return (T) binaryFormatter.Deserialize(memoryStream);
}
}
}
}
@syaifulnizamyahya
Copy link

Nice gist. Unfortunately doesnt work on UWP. failed at BinaryFormatter. Know any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment