/// <summary> /// DataTable转List /// </summary> /// <typeparam name="T">转换类型</typeparam> /// <param name="dt">数据源</param> /// <returns></returns> public static List <T> EmitToList <T>(this DataTable dt) { //确认参数有效 if (dt == null) { return(null); } List <T> list = new List <T>(); var objBuilder = EmitHelper.CreateBuilder(typeof(T)); for (int i = 0; i < dt.Rows.Count; i++) { //创建泛型对象 T _t = (T)objBuilder(); //获取对象所有属性 PropertyInfo[] propertyInfo = _t.GetType().GetProperties(); for (int j = 0; j < dt.Columns.Count; j++) { foreach (PropertyInfo info in propertyInfo) { //属性名称和列名相同时赋值 if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper())) { if (dt.Rows[i][j] != DBNull.Value) { info.SetValue(_t, dt.Rows[i][j], null); } else { info.SetValue(_t, null, null); } break; } } } list.Add(_t); } return(list); }