首页 > 代码库 > 反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>
反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>
将DataTable集合反射获取 List<M>
/// <summary> /// 根据DataTable集合反射获取 List<M> /// </summary> /// <typeparam name="M">泛型实体</typeparam> /// <param name="dt">DataTable</param> /// <returns>实体集合</returns> private static List<M> SetValueRow<M>(DataTable dt) where M : new() { List<M> list = new List<M>(); Type type; PropertyInfo p; M m; foreach (DataRow row in dt.Rows) { m = new M(); type = m.GetType(); foreach (DataColumn col in dt.Columns) { //获取一个字段的属性 p = type.GetProperty(col.ColumnName); //实体中无对应属性 if (p == null) continue; string colDbType = row[col.ColumnName].GetType().FullName; //结果集单元格中的值不为空时才赋值 if (colDbType != "System.DBNull") { switch (p.PropertyType.FullName) { case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null); break; case "System.Int32": p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null); break; case "System.Int16": p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null); break; case "System.String": p.SetValue(m, Convert.ToString(row[col.ColumnName]), null); break; case "System.Decimal": p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null); break; case "System.DateTime": p.SetValue(m, row[col.ColumnName], null); break; case "System.Double": p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null); break; case "System.Boolean": p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null); break; case "System.Byte": p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null); break; default: p.SetValue(m, row[col.ColumnName], null); break; } } } list.Add(m); } return list; }
将IList集合类转换成DataTable
/// <summary> /// 将IList集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable IListToDataTable(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; }
将List<M>集合类转换成DataTable
/// <summary> /// 将List<M>集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable IListToDataTable<M>(List<M> list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; }
反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。