平时开发中经常要用到序列化和反序列化技术,写了一个工具类实现,共享给大家。
直接上代码
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Gren.FrameWork.Common
6 {
7 /// <summary>
8 /// 序列化、反序列化工具
9 /// </summary>
10 public class SerializationUtility
11 {
12 /// <summary>
13 /// 将对象序列化为XML格式的字符串
14 /// </summary>
15 public static string SerializeByXml<T>(T obj)
16 {
17 string xml = string.Empty;
18
19 if (obj != null)
20 {
21 StringBuilder sb = new StringBuilder();
22
23 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
24
25 using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
26 {
27 xs.Serialize(sw, obj);
28 }
29 xml = sb.ToString();
30 }
31
32 return xml;
33 }
34
35 /// <summary>
36 /// 将XML格式的字符串反序列化为对象实例
37 /// </summary>
38 public static T DeserializeByXml<T>(string xml)
39 {
40 object obj = null;
41
42 if (!string.IsNullOrEmpty(xml))
43 {
44 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
45
46 using (System.IO.StringReader sr = new System.IO.StringReader(xml))
47 {
48 obj = xs.Deserialize(sr);
49 }
50 }
51 return (T)obj;
52 }
53
54 /// <summary>
55 /// 将对象序列化为二进制数组
56 /// </summary>
57 public static byte[] SerializeToByte<T>(T obj)
58 {
59 byte[] buffer = null;
60
61 if (obj != null)
62 {
63 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
64
65 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
66 {
67 fmt.Serialize(ms, obj);
68 buffer = ms.ToArray();
69 }
70 }
71
72 return buffer;
73 }
74
75 /// <summary>
76 /// 将二进制数组反序列化为对象实例
77 /// </summary>
78 public static T DeserializeFromByte<T>(byte[] buffer)
79 {
80 object obj = null;
81
82 if (buffer != null)
83 {
84 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
85
86 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer))
87 {
88 obj = fmt.Deserialize(ms);
89 }
90 }
91
92 return (T)obj;
93 }
94
95 /// <summary>
96 /// 将对象序列化为base64String
97 /// </summary>
98 public static string SerializeByBinary<T>(T obj)
99 {
100 string base64String = string.Empty;
101
102 if (obj != null)
103 {
104 byte[] buffer = SerializeToByte<T>(obj);
105 base64String = Convert.ToBase64String(buffer);
106 }
107
108 return base64String;
109 }
110
111 /// <summary>
112 /// 将base64String反序列化为对象实例
113 /// </summary>
114 public static T DeserializeByBinary<T>(string base64String)
115 {
116 object obj = null;
117
118 if (!string.IsNullOrEmpty(base64String))
119 {
120 byte[] buffer = Convert.FromBase64String(base64String);
121 obj = DeserializeFromByte<T>(buffer);
122 }
123
124 return (T)obj;
125 }
126
127 }
128 }
129
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Gren.FrameWork.Common
6 {
7 /// <summary>
8 /// 序列化、反序列化工具
9 /// </summary>
10 public class SerializationUtility
11 {
12 /// <summary>
13 /// 将对象序列化为XML格式的字符串
14 /// </summary>
15 public static string SerializeByXml<T>(T obj)
16 {
17 string xml = string.Empty;
18
19 if (obj != null)
20 {
21 StringBuilder sb = new StringBuilder();
22
23 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
24
25 using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
26 {
27 xs.Serialize(sw, obj);
28 }
29 xml = sb.ToString();
30 }
31
32 return xml;
33 }
34
35 /// <summary>
36 /// 将XML格式的字符串反序列化为对象实例
37 /// </summary>
38 public static T DeserializeByXml<T>(string xml)
39 {
40 object obj = null;
41
42 if (!string.IsNullOrEmpty(xml))
43 {
44 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
45
46 using (System.IO.StringReader sr = new System.IO.StringReader(xml))
47 {
48 obj = xs.Deserialize(sr);
49 }
50 }
51 return (T)obj;
52 }
53
54 /// <summary>
55 /// 将对象序列化为二进制数组
56 /// </summary>
57 public static byte[] SerializeToByte<T>(T obj)
58 {
59 byte[] buffer = null;
60
61 if (obj != null)
62 {
63 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
64
65 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
66 {
67 fmt.Serialize(ms, obj);
68 buffer = ms.ToArray();
69 }
70 }
71
72 return buffer;
73 }
74
75 /// <summary>
76 /// 将二进制数组反序列化为对象实例
77 /// </summary>
78 public static T DeserializeFromByte<T>(byte[] buffer)
79 {
80 object obj = null;
81
82 if (buffer != null)
83 {
84 System.Runtime.Serialization.IFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
85
86 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer))
87 {
88 obj = fmt.Deserialize(ms);
89 }
90 }
91
92 return (T)obj;
93 }
94
95 /// <summary>
96 /// 将对象序列化为base64String
97 /// </summary>
98 public static string SerializeByBinary<T>(T obj)
99 {
100 string base64String = string.Empty;
101
102 if (obj != null)
103 {
104 byte[] buffer = SerializeToByte<T>(obj);
105 base64String = Convert.ToBase64String(buffer);
106 }
107
108 return base64String;
109 }
110
111 /// <summary>
112 /// 将base64String反序列化为对象实例
113 /// </summary>
114 public static T DeserializeByBinary<T>(string base64String)
115 {
116 object obj = null;
117
118 if (!string.IsNullOrEmpty(base64String))
119 {
120 byte[] buffer = Convert.FromBase64String(base64String);
121 obj = DeserializeFromByte<T>(buffer);
122 }
123
124 return (T)obj;
125 }
126
127 }
128 }
129
命名有点不规范,主要是以前没注意,后来在项目中使用了,再改的话会影响现有项目。大家要用的话自己改改名称,代码很简单。
有什么地方不合理或者可以优化的请给我留言,谢谢!