Exemple #1
0
        /// <summary>
        ///     Request.QueryString
        /// </summary>
        public static T QS <T>(string parmsName, T defValue, Encoding encoding)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            if (HttpContext.Current == null)
            {
                return(defValue);
            }
            var requestType = HttpContext.Current.Request.GetType();
            var property    = requestType.GetProperty("QueryStringBytes", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic);
            //var queryBytes = (byte[])property.GetValue(HttpContext.Current.Request, null);
            var queryBytes = (byte[])PropertyGetCacheManger.Cache(property, HttpContext.Current.Request);
            var parms      = HttpUtility.UrlDecode(queryBytes, encoding);

            if (string.IsNullOrWhiteSpace(parms))
            {
                return(defValue);
            }

            parmsName += "=";
            foreach (var str in parms.Split('&'))
            {
                if (str.ToLower().StartsWith(parmsName))
                {
                    return(ConvertHelper.ConvertType(str.Substring(parmsName.Length), defValue));
                }
            }
            return(defValue);
        }
        /// <summary>
        ///     Insert将实体类的赋值,转成表达式树
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="entity">被赋值的实体</param>
        internal void AssignInsert <TEntity>(TEntity entity) where TEntity : class, new()
        {
            if (entity == null)
            {
                return;
            }
            var oParameter = Expression.Parameter(entity.GetType(), "o");
            var lstAssign  = new List <Expression>();

            //  迭代实体赋值情况
            //  这里没有限定标识字段,允许客户手动设置标识数据
            foreach (var kic in SetMap.PhysicsMap.MapList.Where(o => o.Value.Field.IsMap && !o.Value.Field.IsFun && o.Value.Field.InsertStatusType == StatusType.CanWrite))
            {
                var obj = PropertyGetCacheManger.Cache(kic.Key, entity);
                if (obj == null)
                {
                    continue;
                }
                var member = Expression.MakeMemberAccess(oParameter, kic.Key);

                var ass = Expression.Assign(member, Expression.Convert(Expression.Constant(obj), kic.Key.PropertyType));
                lstAssign.Add(ass);
            }
            ExpAssign = ExpressionHelper.MergeBlockExpression(lstAssign.ToArray());
        }
Exemple #3
0
        /// <summary>
        ///     将集合类转换成DataTable
        /// </summary>
        /// <param name="list">集合</param>
        /// <returns></returns>
        public static DataTable ToTable(this IList list)
        {
            var result = new DataTable();

            if (list.Count <= 0)
            {
                return(result);
            }

            var propertys = list[0].GetType().GetProperties();

            foreach (var pi in propertys)
            {
                result.Columns.Add(pi.Name, pi.PropertyType);
            }

            foreach (var entity in list)
            {
                var tempList = new ArrayList();
                foreach (var obj in propertys.Select(pi => PropertyGetCacheManger.Cache(pi, entity)))
                {
                    tempList.Add(obj);
                }
                var array = tempList.ToArray();
                result.LoadDataRow(array, true);
            }
            return(result);
        }
Exemple #4
0
        /// <summary>
        ///     将泛型集合类转换成DataTable
        /// </summary>
        /// <param name="list">集合</param>
        /// <param name="propertyName">需要返回的列的列名</param>
        /// <returns>数据集(表)</returns>
        public static DataTable ToTable(this IList list, params string[] propertyName)
        {
            var propertyNameList = new List <string>();

            if (propertyName != null)
            {
                propertyNameList.AddRange(propertyName);
            }

            var result = new DataTable();

            if (list.Count <= 0)
            {
                return(result);
            }
            var propertys = list[0].GetType().GetProperties();

            foreach (var pi in propertys)
            {
                if (propertyNameList.Count == 0)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }
                else
                {
                    if (propertyNameList.Contains(pi.Name))
                    {
                        result.Columns.Add(pi.Name, pi.PropertyType);
                    }
                }
            }

            foreach (var entity in list)
            {
                var tempList = new ArrayList();
                foreach (var pi in propertys)
                {
                    if (propertyNameList.Count == 0)
                    {
                        //var obj = pi.GetValue(info, null);
                        var obj = PropertyGetCacheManger.Cache(pi, entity);
                        tempList.Add(obj);
                    }
                    else
                    {
                        if (!propertyNameList.Contains(pi.Name))
                        {
                            continue;
                        }
                        //var obj = pi.GetValue(entity, null);
                        var obj = PropertyGetCacheManger.Cache(pi, entity);
                        tempList.Add(obj);
                    }
                }
                var array = tempList.ToArray();
                result.LoadDataRow(array, true);
            }
            return(result);
        }
        /// <summary>
        ///     插入
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="identity">返回标识字段(如果设置的话)</param>
        public int Insert(TEntity entity, out int identity)
        {
            Check.NotNull(entity, "插入操作时,entity参数不能为空!");

            var result = Insert(entity, true);

            // 获取标识字段
            identity = ConvertHelper.ConvertType(PropertyGetCacheManger.Cache(SetMap.PhysicsMap.DbGeneratedFields.Key, entity), 0);
            return(result);
        }
 /// <summary>
 ///     存储过程创建SQL 输入、输出参数化
 /// </summary>
 /// <typeparam name="TEntity">实体类</typeparam>
 /// <param name="map">实体类结构</param>
 /// <param name="entity">实体类</param>
 public List<DbParameter> InitParam<TEntity>(SetPhysicsMap map, TEntity entity) where TEntity : class, new()
 {
     var lstParam = new List<DbParameter>();
     if (entity == null) { return lstParam; }
     foreach (var kic in map.MapList.Where(o => o.Value.Field.IsInParam || o.Value.Field.IsOutParam))
     {
         var obj = PropertyGetCacheManger.Cache(kic.Key, entity);
         lstParam.Add(CreateDbParam(kic.Value.Field.Name, obj, kic.Key.PropertyType, kic.Value.Field.IsOutParam));
     }
     return lstParam;
 }
Exemple #7
0
        /// <summary>
        ///     动态添加List元素
        /// </summary>
        /// <param name="propertyInfo">字段类型</param>
        /// <param name="entity">所属实体变量</param>
        /// <param name="methodName">方法名称,默认是Add</param>
        private static void AddItem(PropertyInfo propertyInfo, object entity, string methodName = "Add")
        {
            // 非泛型,退出执行
            if (!propertyInfo.PropertyType.IsGenericType)
            {
                return;
            }
            var lstVal = PropertyGetCacheManger.Cache(propertyInfo, entity);

            // 空时,反射创建
            if (lstVal == null)
            {
                lstVal = InstanceCacheManger.Cache(propertyInfo.PropertyType);
                PropertySetCacheManger.Cache(propertyInfo, entity, lstVal);
            }

            // 获取执行方法
            var method = propertyInfo.PropertyType.GetMethod(methodName);

            if (method == null)
            {
                return;
            }

            var lstParamInstance = new List <object>();

            foreach (var parameterInfo in method.GetParameters())
            {
                // 反射创建子元素
                object item;
                if (parameterInfo.ParameterType == typeof(string))
                {
                    item = string.Empty;
                }
                else
                {
                    item = parameterInfo.ParameterType.IsClass ? InstanceCacheManger.Cache(parameterInfo.ParameterType) : 0;
                }
                lstParamInstance.Add(item);
            }
            method.Invoke(lstVal, lstParamInstance.ToArray());
            foreach (var field in lstVal.GetType().GetFields())
            {
                AddItem(field, lstVal);
            }
            foreach (var property in lstVal.GetType().GetProperties())
            {
                AddItem(property, lstVal);
            }
        }
Exemple #8
0
        static void TestGetValueCache(int count = 1000000)
        {
            var user         = new UserVO();
            var propertyInfo = user.GetType().GetProperty("UserName");

            SpeedTest.ConsoleTime("手动取值", count, () =>
            {
                var a = user.UserName;
            });
            SpeedTest.ConsoleTime("表达式树取值", count, () =>
            {
                var a = PropertyGetCacheManger.Cache(propertyInfo, user);
            });
            SpeedTest.ConsoleTime("反射取值", count, () =>
            {
                var a = propertyInfo.GetValue(user, null);
            });
        }
        /// <summary>
        ///     检测实体类值状况
        /// </summary>
        /// <param name="dicError">返回错误消息,key:属性名称;vakue:错误消息</param>
        /// <param name="entity">要检测的实体</param>
        public static bool Check <TEntity>(this TEntity entity, out Dictionary <string, List <string> > dicError) where TEntity : IVerification
        {
            dicError = new Dictionary <string, List <string> >();
            var map = SetMapCacheManger.Cache(typeof(TEntity));

            foreach (var kic in map.MapList)
            {
                var lstError = new List <string>();
                var value    = PropertyGetCacheManger.Cache(kic.Key, entity);
                // 是否必填
                if (kic.Value.Required != null && !kic.Value.Required.IsValid(value))
                {
                    lstError.Add(kic.Value.Required.ErrorMessage);
                }

                //if (value == null) { continue; }

                // 字符串长度判断
                if (kic.Value.StringLength != null && !kic.Value.StringLength.IsValid(value))
                {
                    lstError.Add(kic.Value.StringLength.ErrorMessage);
                }

                // 值的长度
                if (kic.Value.Range != null && !kic.Value.Range.IsValid(value))
                {
                    lstError.Add(kic.Value.Range.ErrorMessage);
                }

                // 正则
                if (kic.Value.RegularExpression != null && !kic.Value.RegularExpression.IsValid(value))
                {
                    lstError.Add(kic.Value.RegularExpression.ErrorMessage);
                }

                if (lstError.Count > 0)
                {
                    dicError.Add(kic.Key.Name, lstError);
                }
            }
            return(dicError.Count == 0);
        }
Exemple #10
0
 /// <summary>
 ///     查找对象属性值
 /// </summary>
 /// <typeparam name="TEntity">实体类</typeparam>
 /// <typeparam name="T">返回值类型</typeparam>
 /// <param name="entity">当前实体类</param>
 /// <param name="propertyName">属性名</param>
 /// <param name="defValue">默认值</param>
 public static T GetValue <TEntity, T>(TEntity entity, string propertyName, T defValue = default(T)) where TEntity : class
 {
     if (entity == null)
     {
         return(defValue);
     }
     foreach (var property in entity.GetType().GetProperties())
     {
         if (property.Name != propertyName)
         {
             continue;
         }
         if (!property.CanRead)
         {
             return(defValue);
         }
         return(ConvertType(PropertyGetCacheManger.Cache(property, entity), defValue));
     }
     return(defValue);
 }
        /// <summary>
        ///     Update将实体类的赋值,转成表达式树
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="entity">被赋值的实体</param>
        internal void AssignUpdate <TEntity>(TEntity entity) where TEntity : class, new()
        {
            if (entity == null)
            {
                return;
            }
            var oParameter = Expression.Parameter(entity.GetType(), "o");
            var lstAssign  = new List <Expression>();

            //  迭代实体赋值情况
            foreach (var kic in SetMap.PhysicsMap.MapList.Where(o => o.Value.Field.IsMap && !o.Value.Field.IsFun && o.Value.Field.InsertStatusType == StatusType.CanWrite))
            {
                var obj = PropertyGetCacheManger.Cache(kic.Key, entity);
                if (obj == null)
                {
                    continue;
                }
                var member = Expression.MakeMemberAccess(oParameter, kic.Key);

                // 主键、只读条件、主键状态下,转为条件状态
                if ((kic.Value.Field.UpdateStatusType == StatusType.ReadCondition || kic.Value.Field.IsPrimaryKey))
                {
                    // 当前条件已存在该值时,跳过
                    if (new GetMemberVisitor().Visit(ExpWhere).Any(o => o.Member == kic.Key))
                    {
                        continue;
                    }
                    var expCondiction = ExpressionHelper.CreateBinaryExpression <TEntity>(obj, member);
                    ExpWhere = ExpressionHelper.MergeAndAlsoExpression((Expression <Func <TEntity, bool> >)ExpWhere, expCondiction);
                }
                else
                {
                    var ass = Expression.Assign(member, Expression.Convert(Expression.Constant(obj), kic.Key.PropertyType));
                    lstAssign.Add(ass);
                }
            }
            ExpAssign = ExpressionHelper.MergeBlockExpression(lstAssign.ToArray());
        }
        /// <summary>
        ///     插入(支持延迟加载)会自动赋值标识字段
        /// </summary>
        /// <param name="entity"></param>
        public int Insert(TEntity entity)
        {
            Check.NotNull(entity, "插入操作时,参数不能为空!");

            // 更新本地缓存
            var lst = Cache.ToList();

            // 标识字段是否有值
            var indexHaveValue = _set.SetMap.PhysicsMap.DbGeneratedFields.Key != null && PropertyGetCacheManger.Cache(_set.SetMap.PhysicsMap.DbGeneratedFields.Key, entity) != null;

            _set.Insert(entity, !indexHaveValue);

            lst.Add(entity);
            _dataCache.Update(lst);
            return(1);
        }