List与DataTable相互转换

   /// <summary>
        /// 实体转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="tolist"></param>
        /// <param name="fromlist"></param>
        /// <returns></returns>
        public static T AutoMap<T,U>(T tolist,U fromlist)
        {
            //返回当前对象的所有公共属性
            PropertyInfo[] topropertys = typeof(T).GetProperties();
            PropertyInfo[] frompropertys = typeof(U).GetProperties();
            foreach(PropertyInfo toproperty in topropertys)
            {
                foreach(PropertyInfo fromproperty in frompropertys)
                {

                      if(toproperty.Name== fromproperty.Name)
                      {
                        toproperty.SetValue(tolist, fromproperty.GetValue(fromlist));
                      }

                }
            }
            return tolist;
        }  

/// <summary>
        /// datatable转list
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
        {
            List<Dictionary<string, object>> list
                 = new List<Dictionary<string, object>>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                list.Add(dic);
            }
            return list;
        }

        /// <summary>
        /// list 转datatable
        /// </summary>
        private static DataTable ToDataTable<T>(List<T> list)
        {
            var tb = new DataTable(typeof(T).Name);
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //表列名
            foreach (PropertyInfo prop in props)
            {
                Type t = GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }
            //遍历数据
            foreach (T item in list)
            {
                var values = new object[props.Length];

                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return tb;
        }
        /// <summary>
        /// 如果类型可以为空,则返回基础类型,否则返回类型
        /// </summary>
        public static Type GetCoreType(Type t)
        {
            if (t != null && IsNullable(t))
            {
                if (!t.IsValueType)
                {
                    return t;
                }
                else
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            else
            {
                return t;
            }
        }
        /// <summary>
        /// 指定类型可为空
        /// </summary>
        public static bool IsNullable(Type t)
        {
            return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
        }

 

posted @ 2021-06-03 18:35  万万万W  阅读(291)  评论(0编辑  收藏  举报