-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySerializer.cs
29 lines (22 loc) · 1006 Bytes
/
BinarySerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Edelstein.Assets.Management;
public static class BinarySerializer
{
public static T Deserialize<T>(Stream dataStream, BinaryFormatter binaryFormatter) =>
(T)binaryFormatter.Deserialize(dataStream);
public static T Deserialize<T, TBinder>(Stream dataStream)
where TBinder : SerializationBinder, new()
{
BinaryFormatter binaryFormatter = new() { Binder = new TBinder() };
return (T)binaryFormatter.Deserialize(dataStream);
}
public static void Serialize<T>(T data, Stream outputStream, BinaryFormatter binaryFormatter) =>
binaryFormatter.Serialize(outputStream, data!);
public static void Serialize<T, TBinder>(T data, Stream outputStream)
where TBinder : SerializationBinder, new()
{
BinaryFormatter binaryFormatter = new() { Binder = new TBinder() };
binaryFormatter.Serialize(outputStream, data!);
}
}