好久没有写博客了,今天终于有点时间来继续写写东西,今天我们来看一下Serializer序列化类,在CommunityServer中这个实例的存储方法很值得我们学习,在加入属性是在数据库中就不需要添加字段名。能够实现这种功能的做法就是通过序列化和反序列化来完成这项功能,将属性序列化为XML格式的文本存储与数据库中的一个NText字段中。
我们来看看这个类的代码,上面有我的注释,获取有些地方说得不正确,那也没办法啦!
序列化
1 /**//// <summary>
2 /// 序列化类。
3 /// </summary>
4 public class Serializer
5 {
6 //防止被实例化。
7 private Serializer()
8 {
9
10 }
11 /**//// <summary>
12 /// 静态构造函数仅在设置CanBinarySerialize值中使用一次。
13 /// </summary>
14 static Serializer()
15 {
16 SecurityPermission sp = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
17 try
18 {
19 sp.Demand();
20 CanBinarySerialize = true;
21 }
22 catch (SecurityException)
23 {
24 CanBinarySerialize = false;
25 }
26 }
27 /**//// <summary>
28 /// 获取二进制序列化是否被使用。
29 /// </summary>
30 public static readonly bool CanBinarySerialize;
31 /**//// <summary>
32 /// 将对象转化成二进制的数组。
33 /// </summary>
34 /// <param name="objectToConvert">用于转化的对象。</param>
35 /// <returns>返回转化后的数组,如果CanBinarySerialize为false则返回null。</returns>
36 public static byte[] ConvertToBytes(object objectToConvert)
37 {
38 byte[] byteArray = null;
39
40 if (CanBinarySerialize)
41 {
42 BinaryFormatter binaryFormatter = new BinaryFormatter();
43 using (MemoryStream ms = new MemoryStream())
44 {
45
46 binaryFormatter.Serialize(ms, objectToConvert);
47 //设置是内存存储位置为0。
48 ms.Position = 0;
49 //读取数组。
50 byteArray = new Byte[ms.Length];
51 ms.Read(byteArray, 0, byteArray.Length);
52 ms.Close();
53 }
54 }
55 return byteArray;
56 }
57 /**//// <summary>
58 /// 将对象以二进制形式存储到硬盘中。
59 /// </summary>
60 /// <param name="objectToSave">用于保存的对象。</param>
61 /// <param name="path">文件路径。</param>
62 /// <returns>如果存储成功则返回true,否则返回false。</returns>
63 public static bool SaveAsBinary(object objectToSave, string path)
64 {
65 if (objectToSave != null && CanBinarySerialize)
66 {
67 byte[] ba = ConvertToBytes(objectToSave);
68 if (ba != null)
69 {
70 using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
71 {
72 using (BinaryWriter bw = new BinaryWriter(fs))
73 {
74 bw.Write(ba);
75 return true;
76 }
77 }
78 }
79 }
80 return false;
81 }
82 /**//// <summary>
83 ///将对象转化为XML格式文件,该对象必须用[Serialize]标记,否则将抛出错误。
84 /// </summary>
85 /// <param name="objectToConvert">用于序列化的标记。</param>
86 /// <returns>返回XML文本,如果对象为空则返回null。</returns>
87 public static string ConvertToString(object objectToConvert)
88 {
89 string xml = null;
90
91 if (objectToConvert != null)
92 {
93 //获取当前序列化对象。
94 Type t = objectToConvert.GetType();
95
96 XmlSerializer ser = new XmlSerializer(t);
97 //将序列化的结果保存到XML中。
98 using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
99 {
100 ser.Serialize(writer, objectToConvert);
101 xml = writer.ToString();
102 writer.Close();
103 }
104 }
105
106 return xml;
107 }
108 /**//// <summary>
109 /// 将对象序列化后以XML格式存储于文件中。
110 /// </summary>
111 /// <param name="objectToConvert">用于序列化的对象。</param>
112 /// <param name="path">用于存储的文件路径。</param>
113 public static void SaveAsXML(object objectToConvert, string path)
114 {
115 if (objectToConvert != null)
116 {
117 Type t = objectToConvert.GetType();
118
119 XmlSerializer ser = new XmlSerializer(t);
120
121 using (StreamWriter writer = new StreamWriter(path))
122 {
123 ser.Serialize(writer, objectToConvert);
124 writer.Close();
125 }
126 }
127
128 }
129 /**//// <summary>
130 /// 将一个二进制的数组转化为对象,必须通过类型转化自己想得到的相应对象。如果数组为空则返回空。
131 /// </summary>
132 /// <param name="byteArray">用于转化的二进制数组。</param>
133 /// <returns>返回转化后的对象实例,如果数组为空,则返回空对象。</returns>
134 public static object ConvertToObject(byte[] byteArray)
135 {
136 object convertedObject = null;
137 if (CanBinarySerialize && byteArray != null && byteArray.Length > 0)
138 {
139 BinaryFormatter binaryFormatter = new BinaryFormatter();
140 using (MemoryStream ms =
1 /**//// <summary>
2 /// 序列化类。
3 /// </summary>
4 public class Serializer
5 {
6 //防止被实例化。
7 private Serializer()
8 {
9
10 }
11 /**//// <summary>
12 /// 静态构造函数仅在设置CanBinarySerialize值中使用一次。
13 /// </summary>
14 static Serializer()
15 {
16 SecurityPermission sp = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
17 try
18 {
19 sp.Demand();
20 CanBinarySerialize = true;
21 }
22 catch (SecurityException)
23 {
24 CanBinarySerialize = false;
25 }
26 }
27 /**//// <summary>
28 /// 获取二进制序列化是否被使用。
29 /// </summary>
30 public static readonly bool CanBinarySerialize;
31 /**//// <summary>
32 /// 将对象转化成二进制的数组。
33 /// </summary>
34 /// <param name="objectToConvert">用于转化的对象。</param>
35 /// <returns>返回转化后的数组,如果CanBinarySerialize为false则返回null。</returns>
36 public static byte[] ConvertToBytes(object objectToConvert)
37 {
38 byte[] byteArray = null;
39
40 if (CanBinarySerialize)
41 {
42 BinaryFormatter binaryFormatter = new BinaryFormatter();
43 using (MemoryStream ms = new MemoryStream())
44 {
45
46 binaryFormatter.Serialize(ms, objectToConvert);
47 //设置是内存存储位置为0。
48 ms.Position = 0;
49 //读取数组。
50 byteArray = new Byte[ms.Length];
51 ms.Read(byteArray, 0, byteArray.Length);
52 ms.Close();
53 }
54 }
55 return byteArray;
56 }
57 /**//// <summary>
58 /// 将对象以二进制形式存储到硬盘中。
59 /// </summary>
60 /// <param name="objectToSave">用于保存的对象。</param>
61 /// <param name="path">文件路径。</param>
62 /// <returns>如果存储成功则返回true,否则返回false。</returns>
63 public static bool SaveAsBinary(object objectToSave, string path)
64 {
65 if (objectToSave != null && CanBinarySerialize)
66 {
67 byte[] ba = ConvertToBytes(objectToSave);
68 if (ba != null)
69 {
70 using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
71 {
72 using (BinaryWriter bw = new BinaryWriter(fs))
73 {
74 bw.Write(ba);
75 return true;
76 }
77 }
78 }
79 }
80 return false;
81 }
82 /**//// <summary>
83 ///将对象转化为XML格式文件,该对象必须用[Serialize]标记,否则将抛出错误。
84 /// </summary>
85 /// <param name="objectToConvert">用于序列化的标记。</param>
86 /// <returns>返回XML文本,如果对象为空则返回null。</returns>
87 public static string ConvertToString(object objectToConvert)
88 {
89 string xml = null;
90
91 if (objectToConvert != null)
92 {
93 //获取当前序列化对象。
94 Type t = objectToConvert.GetType();
95
96 XmlSerializer ser = new XmlSerializer(t);
97 //将序列化的结果保存到XML中。
98 using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
99 {
100 ser.Serialize(writer, objectToConvert);
101 xml = writer.ToString();
102 writer.Close();
103 }
104 }
105
106 return xml;
107 }
108 /**//// <summary>
109 /// 将对象序列化后以XML格式存储于文件中。
110 /// </summary>
111 /// <param name="objectToConvert">用于序列化的对象。</param>
112 /// <param name="path">用于存储的文件路径。</param>
113 public static void SaveAsXML(object objectToConvert, string path)
114 {
115 if (objectToConvert != null)
116 {
117 Type t = objectToConvert.GetType();
118
119 XmlSerializer ser = new XmlSerializer(t);
120
121 using (StreamWriter writer = new StreamWriter(path))
122 {
123 ser.Serialize(writer, objectToConvert);
124 writer.Close();
125 }
126 }
127
128 }
129 /**//// <summary>
130 /// 将一个二进制的数组转化为对象,必须通过类型转化自己想得到的相应对象。如果数组为空则返回空。
131 /// </summary>
132 /// <param name="byteArray">用于转化的二进制数组。</param>
133 /// <returns>返回转化后的对象实例,如果数组为空,则返回空对象。</returns>
134 public static object ConvertToObject(byte[] byteArray)
135 {
136 object convertedObject = null;
137 if (CanBinarySerialize && byteArray != null && byteArray.Length > 0)
138 {
139 BinaryFormatter binaryFormatter = new BinaryFormatter();
140 using (MemoryStream ms =