comparison Source/Mapping/MemberMappers/BinarySerialisationToBase64StringMapper.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.IO;
3 using System.Runtime.Serialization.Formatters.Binary;
4
5 namespace BLToolkit.Mapping.MemberMappers
6 {
7 public class BinarySerialisationToBase64StringMapper : MemberMapper
8 {
9 public override void SetValue(object o, object value)
10 {
11 if (value != null) this.MemberAccessor.SetValue(o, BinarydeSerialize(Convert.FromBase64String(value.ToString())));
12 }
13
14 public override object GetValue(object o)
15 {
16 return Convert.ToBase64String(BinarySerialize(this.MemberAccessor.GetValue(o)));
17 }
18
19 static byte[] BinarySerialize(object obj)
20 {
21 if (obj == null) return null;
22 MemoryStream memoryStream = new MemoryStream();
23 BinaryFormatter binaryFormatter = new BinaryFormatter();
24 binaryFormatter.Serialize(memoryStream, obj);
25 memoryStream.Flush();
26 memoryStream.Position = 0;
27 return memoryStream.ToArray();
28 }
29
30 static object BinarydeSerialize(byte[] data)
31 {
32 using (var stream = new MemoryStream(data))
33 {
34 var formatter = new BinaryFormatter();
35 stream.Seek(0, SeekOrigin.Begin);
36 return formatter.Deserialize(stream);
37 }
38 }
39 }
40 }