namespace Mall.Config{/// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class ModelHandler<T> where T : new() { #region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 /// </summary> /// <param name="ds">DataSet</param> /// <returns></returns> public List<T> FillModel(DataSet ds) { if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0) { return null; } else { return FillModel(ds.Tables[0]); } } /// <summary> /// 填充对象列表:用DataSet的第index个表填充实体类 /// </summary> public List<T> FillModel(DataSet ds, int index) { if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0) { return null; } else { return FillModel(ds.Tables[index]); } } /// <summary> /// 填充对象列表:用DataTable填充实体类 /// </summary> public List<T> FillModel(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { return null; } List<T> modelList = new List<T>(); foreach (DataRow dr in dt.Rows) { //T model = (T)Activator.CreateInstance(typeof(T)); T model = new T(); for (int i = 0; i < dr.Table.Columns.Count; i++) { PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); if (propertyInfo != null && dr[i] != DBNull.Value) propertyInfo.SetValue(model, dr[i], null); } modelList.Add(model); } return modelList; } /// <summary> /// 填充对象:用DataRow填充实体类 /// </summary> public T FillModel(DataRow dr) { if (dr == null) { return default(T); } //T model = (T)Activator.CreateInstance(typeof(T)); T model = new T(); for (int i = 0; i < dr.Table.Columns.Count; i++) { PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); if (propertyInfo != null && dr[i] != DBNull.Value) propertyInfo.SetValue(model, dr[i], null); } return model; } #endregion #region 实体类转换成DataTable /// <summary> /// 实体类转换成DataSet /// </summary> /// <param name="modelList">实体类列表</param> /// <returns></returns> public DataSet FillDataSet(List<T> modelList) { if (modelList == null || modelList.Count == 0) { return null; } else { DataSet ds = new DataSet(); ds.Tables.Add(FillDataTable(modelList)); return ds; } } /// <summary> /// 实体类转换成DataTable /// </summary> /// <param name="modelList">实体类列表</param> /// <returns></returns> public DataTable FillDataTable(List<T> modelList) { if (modelList == null || modelList.Count == 0) { return null; } DataTable dt = CreateData(modelList[0]); foreach (T model in modelList) { DataRow dataRow = dt.NewRow(); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null); } dt.Rows.Add(dataRow); } return dt; } /// <summary> /// 根据实体类得到表结构 /// </summary> /// <param name="model">实体类</param> /// <returns></returns> private DataTable CreateData(T model) { DataTable dataTable = new DataTable(typeof(T).Name); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType)); } return dataTable; } /// <summary> /// 实体类转换成DataTable /// </summary> /// <param name="modelList">实体类列表</param> /// <param name="nameList">具体列集合</param> /// <returns></returns> public DataTable FillDataTable(List<T> modelList, List<string> nameList) { if (modelList == null || modelList.Count == 0) { return null; } DataTable dt = CreateData(modelList[0], nameList); foreach (T model in modelList) { DataRow dataRow = dt.NewRow(); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { foreach (string item in nameList) { string[] arr = item.Split(‘,‘); if (arr[0] == propertyInfo.Name) { string temp = ""; switch (arr[2]) { case "Int": temp = propertyInfo.GetValue(model, null).ToString() == "无数据" ? propertyInfo.GetValue(model, null).ToString() : Math.Round((double)propertyInfo.GetValue(model, null), 0).ToString(); break; case "Double": if (propertyInfo.GetValue(model, null).ToString() == "--") temp = "--"; else if (propertyInfo.GetValue(model, null).ToString() == "无数据") temp = "无数据"; else if (propertyInfo.GetValue(model, null).ToString().Contains("%")) temp = propertyInfo.GetValue(model, null).ToString(); else temp = Math.Round((double)propertyInfo.GetValue(model, null), 2).ToString(); break; default: if (propertyInfo.GetValue(model, null) != null) { temp = propertyInfo.GetValue(model, null).ToString(); } break; } dataRow[arr[1]] = temp; } } //if (nameList.Contains(propertyInfo.Name)) //{ // string temp = ""; // temp = (propertyInfo.PropertyType).FullName == "System.Double" // ? Math.Round((double)propertyInfo.GetValue(model, null), 2).ToString() // : propertyInfo.GetValue(model, null).ToString(); // dataRow[propertyInfo.Name] = temp; //} } dt.Rows.Add(dataRow); } return dt; } /// <summary> /// 根据实体类得到表结构 /// </summary> /// <param name="model">实体类</param> /// <param name="nameList"></param> /// <returns></returns> private DataTable CreateData(T model, List<string> nameList) { DataTable dataTable = new DataTable(typeof(T).Name); foreach (string name in nameList) { string[] arr = name.Split(‘,‘); foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { if (arr[0] == propertyInfo.Name) { //dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType)); dataTable.Columns.Add(new DataColumn(arr[1], propertyInfo.PropertyType)); } } } return dataTable; } #endregion } } 调用:Mall.Config.ModelHandler<Bus_OnLine_Custom_Info> handler = new ModelHandler<Bus_OnLine_Custom_Info>();DataTable dt = handler.FillDataTable(online.busonline_list, list);
时间: 2024-10-29 19:11:17