patrick.xu
2020-12-24 5d05df27234fcb2bb9d5179a640c59590009f15a
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
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
 
namespace HalconTools
{
    public class SerializeTool<T>
    {
        public static T SoapFormatterDeserialize(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream(data))
            {
                SoapFormatter formatter = new SoapFormatter();
                return (T) formatter.Deserialize(stream);
            }
        }
 
        public static byte[] SoapFormatterSerialize(T obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                new SoapFormatter().Serialize(stream, obj);
                return stream.ToArray();
            }
        }
 
        public static T XmlSerializerDeserialize(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream(data))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                return (T) serializer.Deserialize(stream);
            }
        }
 
        public static byte[] XmlSerializerSerialize(T obj)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                new XmlSerializer(typeof(T)).Serialize((Stream) stream, obj);
                return stream.ToArray();
            }
        }
    }
}