使用泛型和反射实现:DataTable转List<Model>,以及将DATaRow转Model

使用泛型和反射实现:DataTable转List<Model>,以及将DATaRow转Model

代码如下:

 

    public class Helper
    {
        /// <summary>
        /// 将DataTable中的数据封装到List<Model>集合中。
        /// </summary>
        /// <typeparam name="T">实体类型Model</typeparam>
        /// <param name="dt">需要转换的DataTable数据表</param>
        /// <returns>返回List<Model></returns>
        /// <exception cref="Exception"></exception>
        public static List<T> ToList<T>(DataTable dt)
        {
            try
            {
                List<T> list = new List<T>();
                Type type = typeof(T);
                List<PropertyInfo> plist = new List<PropertyInfo>(type.GetProperties());
                if (dt != null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        //T t = (T)Activator.CreateInstance(type);
                        T t = Activator.CreateInstance<T>();
                        foreach (DataColumn dc in dt.Columns)
                        {
                            PropertyInfo pInfo = plist.Find(p => p.Name == dc.ColumnName);
                            if (pInfo != null)
                            {
                                if (!Convert.IsDBNull(row[pInfo.Name]))
                                {
                                    //pInfo.SetValue(t, row[colm.ColumnName], null);
                                    pInfo.SetValue(t, row[pInfo.Name], null);
                                }
                            }
                        }
                        list.Add(t);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 将DataRow数据行转换为Model实体
        /// </summary>
        /// <typeparam name="T">Model实体的类型</typeparam>
        /// <param name="dr">需要转换的DataRow数据行</param>
        /// <returns>返回Model实体</returns>
        /// <exception cref="Exception"></exception>
        public static T ToModel<T>(DataRow dr)
        {
            try
            {
                Type type = typeof(T);//获得类型
                T model = (T)Activator.CreateInstance(type);//获得实体类
                List<PropertyInfo> plist = type.GetProperties().ToList();
                foreach (DataColumn dc in dr.Table.Columns)
                {
                    PropertyInfo pinfo = plist.Find(p => p.Name == dc.ColumnName);
                    if (pinfo != null)
                    {
                        if (!Convert.IsDBNull(dr[dc.ColumnName]))
                        {
                            pinfo.SetValue(model, dr[dc.ColumnName], null);
                        }
                    }
                }
                return model;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.ToString());
            }
        }
    }

 

posted on 2022-12-14 11:25  hanzq_go  阅读(134)  评论(0编辑  收藏  举报

导航