-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertHelper.cs
More file actions
38 lines (31 loc) · 764 Bytes
/
ConvertHelper.cs
File metadata and controls
38 lines (31 loc) · 764 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
37
38
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CompressedCache
{
public class ConvertHelper
{
public static byte[] ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
using (ms)
{
bf.Serialize(ms, obj);
}
obj = null;
return ms.ToArray();
}
public static Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object)binForm.Deserialize(memStream);
return obj;
}
}
}