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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Vertexwahn
{
public class Serializer
{
public static byte[] SerializeIt(object obj)
{
try
{
MemoryStream bout = new MemoryStream();
BinaryWriter objOut = new BinaryWriter(bout);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(objOut.BaseStream, obj);
objOut.Close();
return bout.ToArray();
}
catch (IOException e)
{
throw new ApplicationException("Serialization failed: " + e.Message);
}
}
public static object DeserializeIt(byte[] data)
{
if (data == null)
return null;
try
{
MemoryStream bin = new MemoryStream(data);
BinaryFormatter formatter = new BinaryFormatter();
object obj = formatter.Deserialize(bin);
bin.Close();
return obj;
}
catch (IOException e)
{
throw new ApplicationException("Deserialization failed: " +
e.Message);
}
catch (Exception e)
{
throw new ApplicationException("Class not found: " + e.Message);
}
}
}
}
|