123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.ComponentModel;
- namespace WMS.Util
- {
- /// <summary>
- /// Table转换实体处理
- /// </summary>
- public class FuncTable2Entity
- {
- /// <summary>
- /// DataTable转换成对象的List集合
- /// </summary>
- /// <typeparam name="T">对象类型</typeparam>
- /// <param name="_tab">DataTable</param>
- /// <returns></returns>
- public static List<T> DataTableToList<T>(DataTable _tab,out string errorMsg) where T : new()
- {
- errorMsg = string.Empty;
- List<T> _list = new List<T>();
- if (_tab == null) return _list;
- System.Reflection.PropertyInfo[] _infos = typeof(T).GetProperties();
- foreach (DataRow _row in _tab.Rows)
- {
- T _t = new T();
- foreach (System.Reflection.PropertyInfo property in _infos)
- if (property.CanWrite && _tab.Columns.Contains(property.Name))
- {
- DataRowToEntity<T>(property, _row, _t, out string msg);
- if (!string.IsNullOrWhiteSpace(msg))
- {
- errorMsg =$"第{_tab.Rows.IndexOf(_row)+1} 行发生错误:{msg}";
- return null;
- }
- }
-
- _list.Add(_t);
- }
- return _list;
- }
- public static List<T> DataTableToList<T>(DataTable _tab) where T : new()
- {
- List<T> _list = new List<T>();
- if (_tab == null) return _list;
- System.Reflection.PropertyInfo[] _infos = typeof(T).GetProperties();
- foreach (DataRow _row in _tab.Rows)
- {
- T _t = new T();
- foreach (System.Reflection.PropertyInfo property in _infos)
- if (property.CanWrite && _tab.Columns.Contains(property.Name))
- DataRowToEntity<T>(property, _row, _t, out string msg);
- _list.Add(_t);
- }
- return _list;
- }
- /// <summary>
- /// DataTable转换成对象的List集合
- /// </summary>
- /// <typeparam name="Parent"></typeparam>
- /// <typeparam name="Child"></typeparam>
- /// <param name="_tab"></param>
- /// <returns></returns>
- public static List<Parent> DataTableToList<Parent, Child>(DataTable _tab)
- where Parent : new()
- where Child : new()
- {
- List<Parent> _list = new List<Parent>();
- if (_tab == null) return _list;
- foreach (DataRow _row in _tab.Rows)
- _list.Add(DataRowToEntity<Parent, Child>(_row));
- return _list;
- }
- /// <summary>
- /// DataTable转换成对象的List集合
- /// </summary>
- /// <typeparam name="Parent"></typeparam>
- /// <typeparam name="ChildParent"></typeparam>
- /// <typeparam name="Child"></typeparam>
- /// <param name="_tab"></param>
- /// <returns></returns>
- public static List<Parent> DataTableToList<Parent, ChildParent, Child>(DataTable _tab)
- where Parent : new()
- where ChildParent : new()
- where Child : new()
- {
- List<Parent> _list = new List<Parent>();
- if (_tab == null) return _list;
- System.Reflection.PropertyInfo[] _infos = typeof(Parent).GetProperties();
- foreach (DataRow _row in _tab.Rows)
- {
- Parent _t = new Parent();
- foreach (System.Reflection.PropertyInfo property in _infos)
- if (property.CanWrite && property.PropertyType.Name == typeof(ChildParent).Name)
- property.SetValue(_t, DataRowToEntity<ChildParent, Child>(_row), null);
- else if (property.CanWrite && _tab.Columns.Contains(property.Name))
- DataRowToEntity<Parent>(property, _row, _t, out string msg);
- _list.Add(_t);
- }
- return _list;
- }
- /// <summary>
- /// DataTable转换成对象的List集合
- /// </summary>
- /// <typeparam name="Parent"></typeparam>
- /// <typeparam name="Child1"></typeparam>
- /// <typeparam name="Child2"></typeparam>
- /// <param name="_tab"></param>
- /// <returns></returns>
- public static List<Parent> DataTableToList2Child<Parent, Child1, Child2>(DataTable _tab)
- where Parent : new()
- where Child1 : new()
- where Child2 : new()
- {
- List<Parent> _list = new List<Parent>();
- if (_tab == null) return _list;
- System.Reflection.PropertyInfo[] _infos = typeof(Parent).GetProperties();
- foreach (DataRow _row in _tab.Rows)
- {
- Parent _t = new Parent();
- foreach (System.Reflection.PropertyInfo property in _infos)
- if (property.CanWrite && property.PropertyType.Name == typeof(Child1).Name)
- property.SetValue(_t, DataRowToEntity<Child1>(_row), null);
- else if (property.CanWrite && property.PropertyType.Name == typeof(Child2).Name)
- property.SetValue(_t, DataRowToEntity<Child2>(_row), null);
- else if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
- DataRowToEntity<Parent>(property, _row, _t, out string msg);
- _list.Add(_t);
- }
- return _list;
- }
- /// <summary>
- /// 把DataRow转换成实体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="_row"></param>
- /// <returns></returns>
- public static T DataRowToEntity<T>(DataRow _row) where T : new()
- {
- T _t = new T();
- foreach (System.Reflection.PropertyInfo property in typeof(T).GetProperties())
- if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
- DataRowToEntity<T>(property, _row, _t, out string msg);
- return _t;
- }
- /// <summary>
- /// 把DataRow转换成实体
- /// </summary>
- /// <typeparam name="Parent"></typeparam>
- /// <typeparam name="Child"></typeparam>
- /// <param name="_row"></param>
- /// <returns></returns>
- public static Parent DataRowToEntity<Parent, Child>(DataRow _row)
- where Parent : new()
- where Child : new()
- {
- Parent _t = new Parent();
- foreach (System.Reflection.PropertyInfo property in typeof(Parent).GetProperties())
- if (property.CanWrite && property.PropertyType.Name == typeof(Child).Name)
- property.SetValue(_t, DataRowToEntity<Child>(_row), null);
- else if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
- DataRowToEntity<Parent>(property, _row, _t ,out string msg);
- return _t;
- }
- /// <summary>
- /// 把DataRow转换成实体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="property"></param>
- /// <param name="_row"></param>
- /// <param name="_t"></param>
- /// <returns></returns>
- public static T DataRowToEntity<T>(System.Reflection.PropertyInfo property, DataRow _row, T _t,out string errorMsg) where T : new()
- {
- errorMsg = string.Empty;
- if (_row[property.Name] is DBNull) return _t;
- try
- {
- var type = property.PropertyType;
- if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- var nc = new NullableConverter(type);
- if (nc.UnderlyingType == _row.Table.Columns[property.Name].DataType)
- {
- property.SetValue(_t, _row[property.Name], null);
- return _t;
- }
- type = nc.UnderlyingType;
- }
- else
- {
- if (type != typeof(string) && type == _row.Table.Columns[property.Name].DataType)
- {
- property.SetValue(_t, _row[property.Name], null);
- return _t;
- }
- }
- if (type == typeof(bool))
- property.SetValue(_t, FuncStr.NullToInt(_row[property.Name]) == 1, null);
- else if (type.IsEnum)
- property.SetValue(_t, Enum.Parse(type, _row[property.Name].ToString()), null);
- else if (type.IsValueType)
- {
- List<Type> typelist = new List<Type>() { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) };
- var _value = new object();
- if (typelist.Any(t => t == type))
- {
- var va = new object[] { _row[property.Name].ToString(), System.Globalization.NumberStyles.Float, null, null };
- var result = type.InvokeMember("TryParse", System.Reflection.BindingFlags.InvokeMethod, null, null, va);
- if ((bool)result == true)
- {
- _value = va[3];
- }
- else
- {
- throw new Exception();
- }
- }
- else
- {
- _value = type.InvokeMember("Parse", System.Reflection.BindingFlags.InvokeMethod, null, null, new object[] { _row[property.Name].ToString() });
- }
- property.SetValue(_t, _value, null);
- }
- else if (type == typeof(string))
- property.SetValue(_t, _row[property.Name].ToString().Trim(), null);
- else property.SetValue(_t, _row[property.Name], null);
- return _t;
- }
- catch (Exception ex)
- {
- errorMsg= string.Format("{0}:{1}[{2}] Value is {3}, 值转换错误!", typeof(T).Name, property.Name, property.PropertyType, _row[property.Name]);
- return default(T);
- }
- }
- }
- }
|