DateTable转化为泛型集合

public class ListUtil
    {
        public static List<T> ToList<T>(DataTable dt)
        {
            List<T> list = new List<T>();
            T t = default(T);
            PropertyInfo[] propertyinfos = null;
            foreach (DataRow dr in dt.Rows)
            {
                t = Activator.CreateInstance<T>();
                propertyinfos = t.GetType().GetProperties();
                foreach (PropertyInfo pro in propertyinfos)
                {
                    if (dt.Columns.Contains(pro.Name))
                    {
                        object value = dr[pro.Name];
                        if (!value.ToString().Equals(""))
                        {
                            pro.SetValue(t, value.ToString(), null);
                        }
                    }
                }
                list.Add(t);
            }
            return  list;
        }
    }

 

posted @ 2017-08-08 15:22  多1份努力~  阅读(167)  评论(0编辑  收藏  举报