Remoting系列之自定义序列化类
【IT168 技术文档】
最近项目开发中的传输数据是围绕Remoting而召开的,所以想把所有的数据实体都定义统一的格式,于是就写了一个基于DataTable的基类BaseModal,其他数据实体全部继承于它。此BaseModal基类还包括了一些其他的自有的属性,例如pageSize每页记录数、currentPage当前页码等等,代码如下:
文件描述#region 文件描述
// -------------------------------------------------------------------------------------------------
// 文 件 名:BaseModel.cs
// 创建日期:2006年06月13日
// 命名空间:Colorful.Model
// 类 型:BaseModel类
// 版 本:1.0.0
// 描 述:数据基本表
// -------------------------------------------------------------------------------------------------
#endregion
![]()
修改记录修改记录#region 修改记录
// ------------------------------------------------------------------------------------------------
// 修改日期:
// 修 改 者:
// 修 改 项:
// ------------------------------------------------------------------------------------------------
#endregion
![]()
using System;
using System.Data;
using System.Text;
using System.Runtime.Serialization;
![]()
namespace Colorful.Model
...{
/**//**//**//// <summary>
/// 数据基本表
/// </summary>
[Serializable]
public class BaseModel : DataTable
...{
protected int index; //记录当前行
protected int pageSize; //每页记录数
protected int currentPage; //当前页码
protected int pages; //总页数
protected long totalRecord; //总记录数
protected string description; //其他描述,及其辅助功能
![]()
public int PageSizepublic int PageSize#region public int PageSize
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 设置或者获取每页记录数
/// </summary>
public int PageSize
...{
get ...{ return pageSize; }
set ...{ pageSize = value; }
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public int CurrentPagepublic int CurrentPage#region public int CurrentPage
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 设置或者获取当前页码
/// </summary>
public int CurrentPage
...{
get ...{ return currentPage; }
set ...{ currentPage = value; }
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public int Pagespublic int Pages#region public int Pages
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 设置或者获取总页数
/// </summary>
public int Pages
...{
get ...{ return pages; }
set ...{ pages = value; }
}
// -----------------------------------------------------------------------------------------
#endregion
![]()
public long TotalRecordpublic long TotalRecord#region public long TotalRecord
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 设置或者获取总记录数
/// </summary>
public long TotalRecord
...{
get ...{ return totalRecord; }
set ...{ totalRecord = value; }
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public string Descriptionpublic string Description#region public string Description
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 其他描述
/// </summary>
public string Description
...{
get ...{ return description; }
set ...{ description = value; }
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
public string GUIDpublic string GUID#region public string GUID
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 设置或获取当前表记录ID
/// </summary>
public string GUID
...{
get
...{
return index == -1 ? "" : Rows[index]["GUID"].ToString();
}
set
...{
if (index > -1)
...{
Rows[index]["GUID"] = value;
}
}
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public BaseModel()public BaseModel()#region public BaseModel()
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 构造函数
/// </summary>
public BaseModel()
...{
index = -1;
pageSize = 20;
currentPage = 1;
pages = 1;
totalRecord = 0;
description = "";
Columns.Add("GUID", typeof(string));
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public bool MoveNext()public bool MoveNext()#region public bool MoveNext()
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 向后移动一行
/// </summary>
public bool MoveNext()
...{
if (index < Rows.Count - 1)
...{
index++;
return true;
}
return false;
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public bool MovePre()public bool MovePre()#region public bool MovePre()
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 向前移动一行
/// </summary>
public bool MovePre()
...{
if (index > 1)
...{
index--;
return true;
}
return false;
}
// --------------------------------------------------------------------------------------
#endregion
![]()
public bool GoToRow(int rowIndex)public bool GoToRow(int rowIndex)#region public bool GoToRow(int rowIndex)
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 转到指定行
/// </summary>
/// <param name="rowIndex">行号,0为第一行</param>
/// <returns>返回是否定位成功</returns>
public bool GoToRow(int rowIndex)
...{
if (rowIndex > -1 && rowIndex < Rows.Count)
...{
this.index = rowIndex;
return true;
}
return false;
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
public bool GoToFirst()public bool GoToFirst()#region public bool GoToFirst()
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 转到首行
/// </summary>
/// <returns>返回是否定位成功</returns>
public bool GoToFirst()
...{
if (Rows.Count > 0)
...{
this.index = 0;
return true;
}
return false;
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
public bool GoToLast()public bool GoToLast()#region public bool GoToLast()
// --------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 转到尾行
/// </summary>
/// <returns>返回是否定位成功</returns>
public bool GoToLast()
...{
if (Rows.Count > 0)
...{
this.index = Rows.Count - 1;
return true;
}
return false;
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public void Insert()public void Insert()#region public void Insert()
// ---------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 插入空行,并把空行当作当前行
/// </summary>
public void Insert()
...{
Rows.Add(NewRow());
index = Rows.Count - 1;
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
public void Delete()public void Delete()#region public void Delete()
// ----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 删除当前行
/// </summary>
public void Delete()
...{
if (index > -1)
...{
Rows[index].Delete();
if (index == Rows.Count)
...{
index = Rows.Count - 1;
}
}
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
}
}
结果发现数据在放序列化的时候出错,发现原来是没有加入序列化和反序列化构造函数,虽然BaseModal继承于DataTable,并且加入了[Serializable]树序列化属性,但是要实现想继承序列化,还是要加入构造函数,于是加入:
protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)#region protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
// -----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 反序列化构造函数
/// </summary>
/// <param name="si">反序列化所需的全部数据</param>
/// <param name="context">目标描述</param>
protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
...{
}
// ----------------------------------------------------------------------------------------
#endregion
![]()
public override void GetObjectData(SerializationInfo info, StreamingContext Context)public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
// -----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 序列化函数
/// </summary>
/// <param name="info">序列化所需的全部数据</param>
/// <param name="context">目标描述</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context):base.GetObjectData(info, context)
...{
}
// -----------------------------------------------------------------------------------------
#endregion
结果编译,没有任何错误,但是类中的属性(pageSize等)值却获取不到,想了很久,发现其实道理跟前面有些类似,因为自己加入的属性根本就没有“告诉”序列化函数去处理,自然而然值就丢失了,于是修改函数如下:
protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)#region protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
// -----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 反序列化构造函数
/// </summary>
/// <param name="si">反序列化所需的全部数据</param>
/// <param name="context">目标描述</param>
protected BaseModel(SerializationInfo info, StreamingContext context) : base(info, context)
...{
index = info.GetInt32("index");
pageSize = info.GetInt32("pageSize");
currentPage = info.GetInt32("currentPage");
pages = info.GetInt32("pages");
totalRecord = info.GetInt64("totalRecord");
description = info.GetString("description");
}
// ---------------------------------------------------------------------------------------
#endregion
![]()
public override void GetObjectData(SerializationInfo info, StreamingContext context)public override void GetObjectData(SerializationInfo info, StreamingContext context)#region public override void GetObjectData(SerializationInfo info, StreamingContext context)
// -----------------------------------------------------------------------------------------
/**//**//**//// <summary>
/// 序列化函数
/// </summary>
/// <param name="info">序列化所需的全部数据</param>
/// <param name="context">目标描述</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
...{
info.AddValue("index", index);
info.AddValue("pageSize", pageSize);
info.AddValue("currentPage", currentPage);
info.AddValue("pages", pages);
info.AddValue("totalRecord", totalRecord);
info.AddValue("description", description);
base.GetObjectData(info, context);
}
// -----------------------------------------------------------------------------------------
#endregion
OK,一切搞定了
0
相关文章
