0
|
1 using System.IO;
|
|
2 using System.Runtime.Serialization.Formatters.Binary;
|
|
3
|
|
4 namespace BLToolkit.Mapping.MemberMappers
|
|
5 {
|
|
6 public class BinarySerialisationMapper : MemberMapper
|
|
7 {
|
|
8 public override void SetValue(object o, object value)
|
|
9 {
|
|
10 if (value != null) this.MemberAccessor.SetValue(o, BinarydesSrialize((byte[])value));
|
|
11 }
|
|
12
|
|
13 public override object GetValue(object o)
|
|
14 {
|
|
15 return BinarySerialize(this.MemberAccessor.GetValue(o));
|
|
16 }
|
|
17
|
|
18 static byte[] BinarySerialize(object obj)
|
|
19 {
|
|
20 if (obj == null) return null;
|
|
21 MemoryStream memoryStream = new MemoryStream();
|
|
22 BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
23 binaryFormatter.Serialize(memoryStream, obj);
|
|
24 memoryStream.Flush();
|
|
25 memoryStream.Position = 0;
|
|
26 return memoryStream.ToArray();
|
|
27 }
|
|
28
|
|
29 static object BinarydesSrialize(byte[] data)
|
|
30 {
|
|
31 using (var stream = new MemoryStream(data))
|
|
32 {
|
|
33 var formatter = new BinaryFormatter();
|
|
34 stream.Seek(0, SeekOrigin.Begin);
|
|
35 return formatter.Deserialize(stream);
|
|
36 }
|
|
37 }
|
|
38 }
|
|
39 }
|