forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryFormatterSerializer.cs
More file actions
36 lines (33 loc) · 900 Bytes
/
BinaryFormatterSerializer.cs
File metadata and controls
36 lines (33 loc) · 900 Bytes
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
30
31
32
33
34
35
36
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ServiceStack.ServiceClient.Web
{
public class BinaryFormatterSerializer
{
public static BinaryFormatterSerializer Instance = new BinaryFormatterSerializer();
readonly BinaryFormatter formatter = new BinaryFormatter();
public byte[] Serialize<TSerializable>(TSerializable graph, bool indentXml)
{
try
{
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, graph);
ms.Flush();
return ms.ToArray();
}
}
catch (Exception ex)
{
throw new SerializationException(
string.Format("Error serializing object of type {0}", graph.GetType().FullName), ex);
}
}
public byte[] Serialize<TSerializable>(TSerializable graph)
{
return Serialize(graph, false);
}
}
}