The representation of how to use MVVM architecture the right way!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
879 B

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SignalsMVVM.HelperClasses
{
public static class BinaryFormatterSerialization
{
static private BinaryFormatter serializer = new BinaryFormatter();
public static T DeepCloneBinaryFormatter<T>(this T obj)
{
return obj.SerializeBinaryFormatter().DeserializeBinaryFormatter<T>();
}
public static byte[] SerializeBinaryFormatter<T>(this T obj)
{
using (var ms = new MemoryStream())
{
serializer.Serialize(ms, obj);
return ms.ToArray();
}
}
public static T DeserializeBinaryFormatter<T>(this byte[] data)
{
using (var ms = new MemoryStream(data, false))
return (T)serializer.Deserialize(ms);
}
}
}