首页 > 代码库 > DateTable转化为泛型集合

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 =http://www.mamicode.com/ dr[pro.Name];
                        if (!value.ToString().Equals(""))
                        {
                            pro.SetValue(t, value.ToString(), null);
                        }
                    }
                }
                list.Add(t);
            }
            return  list;
        }
    }

 

DateTable转化为泛型集合