最近项目开发中的传输数据是围绕Remoting而召开的,所以想把所有的数据实体都定义统一的格式,于是就写了一个基于DataTable的基类BaseModal,其他数据实体全部继承于它。此BaseModal基类还包括了一些其他的自有的属性,例如pageSize每页记录数、currentPage当前页码等等,代码如下:
1
2文件描述#region 文件描述
3// -------------------------------------------------------------------------------------------------
4// 文 件 名:BaseModel.cs
5// 创 建 者:李毅
6// 创建日期:2006年06月13日
7// 命名空间:Colorful.Model
8// 类 型:BaseModel类
9// 版 本:1.0.0
10// 描 述:数据基本表
11// -------------------------------------------------------------------------------------------------
12#endregion
13
14修改记录#region 修改记录
15// -------------------------------------------------------------------------------------------------
16// 修改日期:
17// 修 改 者:
18// 修 改 项:
19// -------------------------------------------------------------------------------------------------
20#endregion
21
22using System;
23using System.Data;
24using System.Text;
25using System.Runtime.Serialization;
26
27namespace Colorful.Model
28{
29 /**//// <summary>
30 /// 数据基本表
31 /// </summary>
32 [Serializable]
33 public class BaseModel : DataTable
34 {
35 protected int index; //记录当前行
36 protected int pageSize; //每页记录数
37 protected int currentPage; //当前页码
38 protected int pages; //总页数
39 protected long totalRecord; //总记录数
40 protected string description; //其他描述,及其辅助功能
41
42 public int PageSize#region public int PageSize
43 // -----------------------------------------------------------------------------------------
44 /**//// <summary>
45 /// 设置或者获取每页记录数
46 /// </summary>
47 public int PageSize
48 {
49 get { return pageSize; }
50 set { pageSize = value; }
51 }
52 // -----------------------------------------------------------------------------------------
53 #endregion
54
55 public int CurrentPage#region public int CurrentPage
56 // -----------------------------------------------------------------------------------------
57 /**//// <summary>
58 /// 设置或者获取当前页码
59 /// </summary>
60 public int CurrentPage
61 {
62 get { return currentPage; }
63 set { currentPage = value; }
64 }
65 // -----------------------------------------------------------------------------------------
66 #endregion
67
68 public int Pages#region public int Pages
69 // -----------------------------------------------------------------------------------------
70 /**//// <summary>
71 /// 设置或者获取总页数
72 /// </summary>
73 public int Pages
74 {
75 get { return pages; }
76 set { pages = value; }
77 }
78 // -----------------------------------------------------------------------------------------
79 #endregion
80
81 public long TotalRecord#region public long TotalRecord
82 // -----------------------------------------------------------------------------------------
83 /**//// <summary>
84 /// 设置或者获取总记录数
85 /// </summary>
86 public long TotalRecord
87 {
88 get { return totalRecord; }
89 set { totalRecord = value; }
90 }
91 // -----------------------------------------------------------------------------------------
92 #endregion
93
94 public string Description#region public string Description
95 // -----------------------------------------------------------------------------------------
96 /**//// <summary>
97 /// 其他描述
98 /// </summary>
99 public string Description
100 {
101 get { return description; }
102 set { description = value; }
103 }
104 // -----------------------------------------------------------------------------------------
105 #endregion
106
107 public string GUID#region public string GUID
108 // -----------------------------------------------------------------------------------------
109 /**//// <summary>
110 /// 设置或获取当前表记录ID
111 /// </summary>
112 public string GUID
113 {
114 get
115 {
116 return index == -1 ? "" : Rows[index]["GUID"].ToString();
117 }
118 set
119 {
120 if (index > -1)
121 {
122 Rows[index]["GUID"] = value;
123 }
124 }
125 }
126 // -----------------------------------------------------------------------------------------
127 #endregion
128
129
130
131 public BaseModel()#region public BaseModel()
132 // -----------------------------------------------------------------------------------------
133 /**//// <summary>
134 /// 构造函数
135 /// </summary>
136 public BaseModel()
137 {
138 index = -1;
139 pageSize = 20;
140 currentPage = 1;
141 pages = 1;
142 totalRecord = 0;
143 description = "";
144
145 Columns.Add("GUID", typeof(string));
146 }
147 // -----------------------------------------------------------------------------------------
148 #endregion
149
150 public bool MoveNext()#region public bool MoveNext()
151 // -----------------------------------------------------------------------------------------
152 /**//// <summary>
153 /// 向后移动一行
154 /// </summary>
155 public bool MoveNext()
156 {
157 if (index < Rows.Count - 1)
158 {
159 index++;
160 return true;
161 }
162 return false;
163 }
164 // -----------------------------------------------------------------------------------------
165 #endregion
166
167 public bool MovePre()#region public bool MovePre()
168 // -----------------------------------------------------------------------------------------
169 /**//// <summary>
170 /// 向前移动一行
171 /// </summary>
172 public bool MovePre()
173 {
174 if (index > 1)
175 {
176 index--;
177 return true;
178 }
179 return false;
180 }
181 // -----------------------------------------------------------------------------------------
182 #endregion
183
184 public bool GoToRow(int rowIndex)#region public bool GoToRow(int rowIndex)
185 // -----------------------------------------------------------------------------------------
186 /**//// <summary>
187 /// 转到指定行
188 /// </summary>
189 /// <param name="rowIndex">行号,0为第一行</param>
190 /// <returns>返回是否定位成功</returns>
191 public bool GoToRow(int rowIndex)
192 {
193 if (rowIndex > -1 && rowIndex < Rows.Count)
194 {
195 this.index = rowIndex;
196 return true;
197 }
198 return false;
199 }
200 // -----------------------------------------------------------------------------------------
201 #endregion
202
203 public bool GoToFirst()#region public bool GoToFirst()
204 // -----------------------------------------------------------------------------------------
205 /**//// <summary>
206 /// 转到首行
207 /// </summary>
208 /// <returns>返回是否定位成功</returns>
209 public bool GoToFirst()
210 {
211 if (Rows.Count > 0)
212 {
213 this.index = 0;
214 return true;
215 }
216 return false;
217 }
218 // -----------------------------------------------------------------------------------------
219 #endregion
220
221 public bool GoToLast()#region public bool GoToLast()
222 // -----------------------------------------------------------------------------------------
223 /**//// <summary>
224 /// 转到尾行
225 /// </summary>
226 /// <returns>返回是否定位成功</returns>
227 public bool GoToLast()
228 {
229 if (Rows.Count > 0)
230 {
231 this.index = Rows.Count - 1;
232 return true;
233 }
234 return false;
235 }
236 // -----------------------------------------------------------------------------------------
237 #endregion
238
239 public void Insert()#region public void Insert()
240 // -----------------------------------------------------------------------------------------
241 /**//// <summary>
242 /// 插入空行,并把空行当作当前行
243 /// </summary>
244 public void Insert()
245 {
246 Rows.Add(NewRow());
247 index = Rows.Count - 1;
248 }
249 // -----------------------------------------------------------------------------------------
250 #endregion
251
252 public void Delete()#region public void Delete()
253 // -----------------------------------------------------------------------------------------
254 /**//// <summary>
255 /// 删除当前行
256 /// </summary>
257 public void Delete()
258 {
259 if (index > -1)
260 {
261 Rows[index].Delete();
262 if (index == Rows.Count)
263 {
264 index = Rows.Count - 1;
265 }
266 }
267 }
268 // -----------------------------------------------------------------------------------------
269 #endregion
270
271 }
272}
273
结果发现数据在放序列化的时候出错,发现原来是没有加入序列化和反序列化构造函数,虽然BaseModal继承于DataTable,并且加入了[Serializable]树序列化属性,但是要实现想继承序列化,还是要加入构造函数,于是加入:
结果编译,没有任何错误,但是类中的属性(pageSize等)值却获取不到,想了很久,发现其实道理跟前面有些类似,因为自己加入的属性根本就没有“告诉”序列化函数去处理,自然而然值就丢失了,于是修改函数如下:
OK,一切搞定了
1
2文件描述#region 文件描述
3// -------------------------------------------------------------------------------------------------
4// 文 件 名:BaseModel.cs
5// 创 建 者:李毅
6// 创建日期:2006年06月13日
7// 命名空间:Colorful.Model
8// 类 型:BaseModel类
9// 版 本:1.0.0
10// 描 述:数据基本表
11// -------------------------------------------------------------------------------------------------
12#endregion
13
14修改记录#region 修改记录
15// -------------------------------------------------------------------------------------------------
16// 修改日期:
17// 修 改 者:
18// 修 改 项:
19// -------------------------------------------------------------------------------------------------
20#endregion
21
22using System;
23using System.Data;
24using System.Text;
25using System.Runtime.Serialization;
26
27namespace Colorful.Model
28{
29 /**//// <summary>
30 /// 数据基本表
31 /// </summary>
32 [Serializable]
33 public class BaseModel : DataTable
34 {
35 protected int index; //记录当前行
36 protected int pageSize; //每页记录数
37 protected int currentPage; //当前页码
38 protected int pages; //总页数
39 protected long totalRecord; //总记录数
40 protected string description; //其他描述,及其辅助功能
41
42 public int PageSize#region public int PageSize
43 // -----------------------------------------------------------------------------------------
44 /**//// <summary>
45 /// 设置或者获取每页记录数
46 /// </summary>
47 public int PageSize
48 {
49 get { return pageSize; }
50 set { pageSize = value; }
51 }
52 // -----------------------------------------------------------------------------------------
53 #endregion
54
55 public int CurrentPage#region public int CurrentPage
56 // -----------------------------------------------------------------------------------------
57 /**//// <summary>
58 /// 设置或者获取当前页码
59 /// </summary>
60 public int CurrentPage
61 {
62 get { return currentPage; }
63 set { currentPage = value; }
64 }
65 // -----------------------------------------------------------------------------------------
66 #endregion
67
68 public int Pages#region public int Pages
69 // -----------------------------------------------------------------------------------------
70 /**//// <summary>
71 /// 设置或者获取总页数
72 /// </summary>
73 public int Pages
74 {
75 get { return pages; }
76 set { pages = value; }
77 }
78 // -----------------------------------------------------------------------------------------
79 #endregion
80
81 public long TotalRecord#region public long TotalRecord
82 // -----------------------------------------------------------------------------------------
83 /**//// <summary>
84 /// 设置或者获取总记录数
85 /// </summary>
86 public long TotalRecord
87 {
88 get { return totalRecord; }
89 set { totalRecord = value; }
90 }
91 // -----------------------------------------------------------------------------------------
92 #endregion
93
94 public string Description#region public string Description
95 // -----------------------------------------------------------------------------------------
96 /**//// <summary>
97 /// 其他描述
98 /// </summary>
99 public string Description
100 {
101 get { return description; }
102 set { description = value; }
103 }
104 // -----------------------------------------------------------------------------------------
105 #endregion
106
107 public string GUID#region public string GUID
108 // -----------------------------------------------------------------------------------------
109 /**//// <summary>
110 /// 设置或获取当前表记录ID
111 /// </summary>
112 public string GUID
113 {
114 get
115 {
116 return index == -1 ? "" : Rows[index]["GUID"].ToString();
117 }
118 set
119 {
120 if (index > -1)
121 {
122 Rows[index]["GUID"] = value;
123 }
124 }
125 }
126 // -----------------------------------------------------------------------------------------
127 #endregion
128
129
130
131 public BaseModel()#region public BaseModel()
132 // -----------------------------------------------------------------------------------------
133 /**//// <summary>
134 /// 构造函数
135 /// </summary>
136 public BaseModel()
137 {
138 index = -1;
139 pageSize = 20;
140 currentPage = 1;
141 pages = 1;
142 totalRecord = 0;
143 description = "";
144
145 Columns.Add("GUID", typeof(string));
146 }
147 // -----------------------------------------------------------------------------------------
148 #endregion
149
150 public bool MoveNext()#region public bool MoveNext()
151 // -----------------------------------------------------------------------------------------
152 /**//// <summary>
153 /// 向后移动一行
154 /// </summary>
155 public bool MoveNext()
156 {
157 if (index < Rows.Count - 1)
158 {
159 index++;
160 return true;
161 }
162 return false;
163 }
164 // -----------------------------------------------------------------------------------------
165 #endregion
166
167 public bool MovePre()#region public bool MovePre()
168 // -----------------------------------------------------------------------------------------
169 /**//// <summary>
170 /// 向前移动一行
171 /// </summary>
172 public bool MovePre()
173 {
174 if (index > 1)
175 {
176 index--;
177 return true;
178 }
179 return false;
180 }
181 // -----------------------------------------------------------------------------------------
182 #endregion
183
184 public bool GoToRow(int rowIndex)#region public bool GoToRow(int rowIndex)
185 // -----------------------------------------------------------------------------------------
186 /**//// <summary>
187 /// 转到指定行
188 /// </summary>
189 /// <param name="rowIndex">行号,0为第一行</param>
190 /// <returns>返回是否定位成功</returns>
191 public bool GoToRow(int rowIndex)
192 {
193 if (rowIndex > -1 && rowIndex < Rows.Count)
194 {
195 this.index = rowIndex;
196 return true;
197 }
198 return false;
199 }
200 // -----------------------------------------------------------------------------------------
201 #endregion
202
203 public bool GoToFirst()#region public bool GoToFirst()
204 // -----------------------------------------------------------------------------------------
205 /**//// <summary>
206 /// 转到首行
207 /// </summary>
208 /// <returns>返回是否定位成功</returns>
209 public bool GoToFirst()
210 {
211 if (Rows.Count > 0)
212 {
213 this.index = 0;
214 return true;
215 }
216 return false;
217 }
218 // -----------------------------------------------------------------------------------------
219 #endregion
220
221 public bool GoToLast()#region public bool GoToLast()
222 // -----------------------------------------------------------------------------------------
223 /**//// <summary>
224 /// 转到尾行
225 /// </summary>
226 /// <returns>返回是否定位成功</returns>
227 public bool GoToLast()
228 {
229 if (Rows.Count > 0)
230 {
231 this.index = Rows.Count - 1;
232 return true;
233 }
234 return false;
235 }
236 // -----------------------------------------------------------------------------------------
237 #endregion
238
239 public void Insert()#region public void Insert()
240 // -----------------------------------------------------------------------------------------
241 /**//// <summary>
242 /// 插入空行,并把空行当作当前行
243 /// </summary>
244 public void Insert()
245 {
246 Rows.Add(NewRow());
247 index = Rows.Count - 1;
248 }
249 // -----------------------------------------------------------------------------------------
250 #endregion
251
252 public void Delete()#region public void Delete()
253 // -----------------------------------------------------------------------------------------
254 /**//// <summary>
255 /// 删除当前行
256 /// </summary>
257 public void Delete()
258 {
259 if (index > -1)
260 {
261 Rows[index].Delete();
262 if (index == Rows.Count)
263 {
264 index = Rows.Count - 1;
265 }
266 }
267 }
268 // -----------------------------------------------------------------------------------------
269 #endregion
270
271 }
272}
273
1protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)#region protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
2 // -----------------------------------------------------------------------------------------
3 /**//// <summary>
4 /// 反序列化构造函数
5 /// </summary>
6 /// <param name="si">反序列化所需的全部数据</param>
7 /// <param name="context">目标描述</param>
8 protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
9 {
10 }
11 // -----------------------------------------------------------------------------------------
12 #endregion
13
14 public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
15 // -----------------------------------------------------------------------------------------
16 /**//// <summary>
17 /// 序列化函数
18 /// </summary>
19 /// <param name="info">序列化所需的全部数据</param>
20 /// <param name="context">目标描述</param>
21 public override void GetObjectData(SerializationInfo info, StreamingContext context):base.GetObjectData(info, context)
22 {
23 }
24 // -----------------------------------------------------------------------------------------
25 #endregion
2 // -----------------------------------------------------------------------------------------
3 /**//// <summary>
4 /// 反序列化构造函数
5 /// </summary>
6 /// <param name="si">反序列化所需的全部数据</param>
7 /// <param name="context">目标描述</param>
8 protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
9 {
10 }
11 // -----------------------------------------------------------------------------------------
12 #endregion
13
14 public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
15 // -----------------------------------------------------------------------------------------
16 /**//// <summary>
17 /// 序列化函数
18 /// </summary>
19 /// <param name="info">序列化所需的全部数据</param>
20 /// <param name="context">目标描述</param>
21 public override void GetObjectData(SerializationInfo info, StreamingContext context):base.GetObjectData(info, context)
22 {
23 }
24 // -----------------------------------------------------------------------------------------
25 #endregion
结果编译,没有任何错误,但是类中的属性(pageSize等)值却获取不到,想了很久,发现其实道理跟前面有些类似,因为自己加入的属性根本就没有“告诉”序列化函数去处理,自然而然值就丢失了,于是修改函数如下:
1protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)#region protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
2 // -----------------------------------------------------------------------------------------
3 /**//// <summary>
4 /// 反序列化构造函数
5 /// </summary>
6 /// <param name="si">反序列化所需的全部数据</param>
7 /// <param name="context">目标描述</param>
8 protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
9 {
10 index = info.GetInt32("index");
11 pageSize = info.GetInt32("pageSize");
12 currentPage = info.GetInt32("currentPage");
13 pages = info.GetInt32("pages");
14 totalRecord = info.GetInt64("totalRecord");
15 description = info.GetString("description");
16 }
17 // -----------------------------------------------------------------------------------------
18 #endregion
19
20 public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
21 // -----------------------------------------------------------------------------------------
22 /**//// <summary>
23 /// 序列化函数
24 /// </summary>
25 /// <param name="info">序列化所需的全部数据</param>
26 /// <param name="context">目标描述</param>
27 public override void GetObjectData(SerializationInfo info, StreamingContext context)
28 {
29 info.AddValue("index", index);
30 info.AddValue("pageSize", pageSize);
31 info.AddValue("currentPage", currentPage);
32 info.AddValue("pages", pages);
33 info.AddValue("totalRecord", totalRecord);
34 info.AddValue("description", description);
35 base.GetObjectData(info, context);
36 }
37 // -----------------------------------------------------------------------------------------
38 #endregion
2 // -----------------------------------------------------------------------------------------
3 /**//// <summary>
4 /// 反序列化构造函数
5 /// </summary>
6 /// <param name="si">反序列化所需的全部数据</param>
7 /// <param name="context">目标描述</param>
8 protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
9 {
10 index = info.GetInt32("index");
11 pageSize = info.GetInt32("pageSize");
12 currentPage = info.GetInt32("currentPage");
13 pages = info.GetInt32("pages");
14 totalRecord = info.GetInt64("totalRecord");
15 description = info.GetString("description");
16 }
17 // -----------------------------------------------------------------------------------------
18 #endregion
19
20 public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
21 // -----------------------------------------------------------------------------------------
22 /**//// <summary>
23 /// 序列化函数
24 /// </summary>
25 /// <param name="info">序列化所需的全部数据</param>
26 /// <param name="context">目标描述</param>
27 public override void GetObjectData(SerializationInfo info, StreamingContext context)
28 {
29 info.AddValue("index", index);
30 info.AddValue("pageSize", pageSize);
31 info.AddValue("currentPage", currentPage);
32 info.AddValue("pages", pages);
33 info.AddValue("totalRecord", totalRecord);
34 info.AddValue("description", description);
35 base.GetObjectData(info, context);
36 }
37 // -----------------------------------------------------------------------------------------
38 #endregion
OK,一切搞定了