示例#1
0
 /// <summary>
 /// Reader 转 List
 /// </summary>
 /// <typeparam name="T">实体</typeparam>
 /// <param name="dr">数据</param>
 /// <returns>List 泛型</returns>
 public static List <T> ReaderToList <T>(IDataReader dr)
 {
     using (dr)
     {
         List <string> list1 = new List <string>(dr.FieldCount);
         for (int i = 0; i < dr.FieldCount; ++i)
         {
             list1.Add(dr.GetName(i).ToLower());
         }
         List <T> list2 = new List <T>();
         while (dr.Read())
         {
             T instance = Activator.CreateInstance <T>();
             foreach (PropertyInfo propertyInfo in instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
             {
                 if (list1.Contains(propertyInfo.Name.ToLower()) && !DataReaderConversion.IsNullOrDBNull(dr[propertyInfo.Name]))
                 {
                     propertyInfo.SetValue((object)instance, DataReaderConversion.HackType(dr[propertyInfo.Name], propertyInfo.PropertyType), (object[])null);
                 }
             }
             list2.Add(instance);
         }
         return(list2);
     }
 }
示例#2
0
        /// <summary>
        /// Reader 转 Model
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="dr">数据</param>
        /// <returns>Model</returns>
        public static T ReaderToModel <T>(IDataReader dr)
        {
            T instance = Activator.CreateInstance <T>();

            while (dr.Read())
            {
                foreach (PropertyInfo propertyInfo in instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
                {
                    if (!DataReaderConversion.IsNullOrDBNull(dr[propertyInfo.Name]))
                    {
                        propertyInfo.SetValue((object)instance, DataReaderConversion.HackType(dr[propertyInfo.Name], propertyInfo.PropertyType), (object[])null);
                    }
                }
            }
            return(instance);
        }