/// <summary> /// 常用方法 /// </summary> public static class Utility { /// <summary> /// DataTable翻页 /// </summary> /// <param name="pageIndex">页序号</param> /// <param name="pageSize">每页显示数量</param> /// <param name="recordCount">总记录数</param> /// <param name="dt">要翻页浏览的DataTable</param> /// <returns></returns> public static DataTable GetPage(int pageIndex, int pageSize, out int recordCount, DataTable dt) { recordCount = 0; if(dt == null) return null; recordCount = dt.Rows.Count; if(pageIndex == 0) pageIndex = 1; DataTable newdt = dt.Clone(); int rowBegin = (pageIndex - 1) * pageSize; int rowEnd = pageIndex * pageSize; if(rowBegin >= dt.Rows.Count) return newdt; if(rowEnd > dt.Rows.Count) rowEnd = dt.Rows.Count; for(int i = rowBegin; i <= rowEnd - 1; i++) { newdt.ImportRow(dt.Rows[i]); } dt.Dispose(); return newdt; } /// <summary> /// 将数据实体序列化,返回序列化后的字符 /// </summary> /// <param name="model">要序列化的数据实体</param> /// <returns></returns> public static string Serialize<TSource>(TSource model) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(model); } /// <summary> /// Json反序列化,用于接收客户端Json后生成对应的对象 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="json">Json字符串</param> /// <returns>Json后生成对应的对象</returns> public static T Deserialize<T>(this string json) { if (json != null && json.Trim() != string.Empty) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<T>(json); } return default(T); } }