Ejemplo n.º 1
0
 /// <summary>
 /// IDataReader(while (dataReader.Read()){}中使用)循环读取转实体(纯反射,无需定义Entity的GetFrom)
 /// </summary>
 /// <typeparam name="T">T</typeparam>
 /// <param name="dataReader">IDataReader对象</param>
 /// <returns></returns>
 public static T ToAnyEntity <T>(this IDataReader dataReader)
 {
     if (dataReader != null && !dataReader.IsClosed)
     {
         var entity = Activator.CreateInstance <T>();
         while (dataReader.Read())
         {
             var t     = typeof(T);
             var count = dataReader.FieldCount;
             for (var i = 0; i < count; i++)
             {
                 if (!BaseUtil.IsNullOrDbNull(dataReader[i]))
                 {
                     var pi = t.GetProperty(dataReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                     if (pi != null)
                     {
                         pi.SetValue(entity, BaseUtil.ChangeType(dataReader[i], pi.PropertyType), null);
                     }
                 }
             }
         }
         dataReader.Close();
         return(entity);
     }
     else
     {
         return(default(T));
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// IDataReader转泛型IList(纯反射,无需定义Entity的GetFrom)
        /// </summary>
        /// <typeparam name="T">泛型实体T</typeparam>
        /// <param name="dataReader">DataReader对象</param>
        /// <returns></returns>
        public static List <T> ToAnyList <T>(this IDataReader dataReader) where T : class, new()
        {
            var ls = new List <T>();
            //获取传入的数据类型
            var t = typeof(T);

            if (dataReader != null && !dataReader.IsClosed)
            {
                //遍历DataReader对象
                while (dataReader.Read())
                {
                    //使用与指定参数匹配最高的构造函数,来创建指定类型的实例
                    var entity = Activator.CreateInstance <T>();
                    for (var i = 0; i < dataReader.FieldCount; i++)
                    {
                        //判断字段值是否为空或不存在的值
                        if (!BaseUtil.IsNullOrDbNull(dataReader[i]))
                        {
                            //匹配字段名
                            var pi = t.GetProperty(dataReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (pi != null)
                            {
                                //绑定实体对象中同名的字段
                                pi.SetValue(entity, BaseUtil.ChangeType(dataReader[i], pi.PropertyType), null);
                            }
                        }
                    }
                    ls.Add(entity);
                }
                dataReader.Close();
            }
            return(ls);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// DataTable转泛型(纯反射,无需定义Entity的GetFrom)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public static List <T> ToAnyList <T>(this DataTable dt) where T : class, new()
        {
            var t          = typeof(T);
            var properties = t.GetProperties();
            var ls         = new List <T>();

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    T entity = new T();
                    foreach (var pi in properties)
                    {
                        var typeName = pi.Name;
                        if (dt.Columns.Contains(typeName))
                        {
                            if (!pi.CanWrite)
                            {
                                continue;
                            }
                            var value = dr[typeName];
                            if (value == DBNull.Value)
                            {
                                continue;
                            }
                            if (pi.PropertyType == typeof(string))
                            {
                                pi.SetValue(entity, value.ToString(), null);
                            }
                            else if (pi.PropertyType == typeof(int))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToInt(value), null);
                            }
                            else if (pi.PropertyType == typeof(int?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableInt(value), null);
                            }
                            else if (pi.PropertyType == typeof(long))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToLong(value), null);
                            }
                            else if (pi.PropertyType == typeof(long?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableLong(value), null);
                            }
                            else if (pi.PropertyType == typeof(DateTime))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToDateTime(value), null);
                            }
                            else if (pi.PropertyType == typeof(DateTime?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableDateTime(value), null);
                            }
                            else if (pi.PropertyType == typeof(float))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToFloat(value), null);
                            }
                            else if (pi.PropertyType == typeof(float?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableFloat(value), null);
                            }
                            else if (pi.PropertyType == typeof(double?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToDouble(value), null);
                            }
                            else if (pi.PropertyType == typeof(double?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableDouble(value), null);
                            }
                            else if (pi.PropertyType == typeof(decimal))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToDecimal(value), null);
                            }
                            else if (pi.PropertyType == typeof(decimal?))
                            {
                                pi.SetValue(entity, BaseUtil.ConvertToNullableDecimal(value), null);
                            }
                            else
                            {
                                pi.SetValue(entity, BaseUtil.ChangeType(value, pi.PropertyType), null);
                            }
                        }
                    }
                    ls.Add(entity);
                }
            }
            return(ls);
        }