首页 > 代码库 > IDataReader转换成list通用方法

IDataReader转换成list通用方法

 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

 

IDataReader转换成list通用方法