public static IList<T> ReaderToList<T>(this IDataReader dr)
        {
            //DateTime dt = DateTime.Now;
            using (dr)
            {
                List<T> list = new List<T>();
                Type modelType = typeof(T);
                int count = dr.FieldCount;
                while (dr.Read())
                {
                    T model = Activator.CreateInstance<T>();

                    for (int i = 0; i < count; i++)
                    {
                        if (!IsNullOrDBNull(dr[i]))
                        {//GetPropertyName
                            PropertyInfo pi = modelType.GetProperty(dr.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (pi != null)
                            {
                                pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
                               // pi.SetValue(model, Convert.ChangeType(dr[i], pi.PropertyType), null);
                            }
                        }
                    }
                    list.Add(model);
                }
              //  DateTime dt2 = DateTime.Now;
               // Logger logInfo = NLog.LogManager.GetLogger("filter");
               // logInfo.Info("List:" + (dt2 - dt).TotalSeconds);
                return list;
            }
        }
View Code

 

posted on 2014-10-15 11:07  随心所意  阅读(1034)  评论(0编辑  收藏  举报