Exemplo n.º 1
0
        /// <summary>
        /// lambda表达式转换sql
        /// 支持的方法:
        /// Contains(支持List.Contains字段与字段Contains字符串),
        /// StartsWith,EndsWith,=,!=,>,>=,小于,小于等于
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="select"></param>
        /// <param name="query"></param>
        /// <param name="orderByClauseList"></param>
        /// <param name="pager"></param>
        /// <param name="unSelect"></param>
        /// <returns></returns>
        private void FillFilter <T>(
            Expression <Func <T, bool> > query     = null,
            Expression <Func <T, object> > select  = null,
            List <OrderByClause> orderByClauseList = null,
            Pager pager = null,
            Expression <Func <T, object> > unSelect = null) where T : class, new()
        {
            dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>();

            #region 处理查询条件
            if (query != null)
            {
                Expression exp = query.Body as Expression;
                HandleFilterExpression(exp);
            }
            #endregion

            #region 处理排序
            if (orderByClauseList != null && orderByClauseList.Count > 0)
            {
                for (int i = 0; i < orderByClauseList.Count; i++)
                {
                    filter.AddOrderBy(orderByClauseList[i].FieldName, orderByClauseList[i].Order);
                }
            }
            #endregion

            #region 处理分页
            if (pager != null)
            {
                filter.InitPage(pager.PageIndex, pager.PageSize);
            }
            #endregion

            #region 处理查询属性
            filter.Select   = select;
            filter.UnSelect = unSelect;
            #endregion
        }
Exemplo n.º 2
0
        /// <summary>
        /// IL生成SetValueMethod内容
        /// 独立出来为共用代码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="il"></param>
        public static void ILGenerateSetValueMethodContent <T>(ILGenerator il)
        {
            LocalBuilder result = il.DeclareLocal(typeof(T));

            il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
            il.Emit(OpCodes.Stloc, result);

            //取出dr中所有的列名集合
            il.DeclareLocal(typeof(DataColumnCollection));
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Callvirt, typeof(DataRow).GetMethod("get_Table"));
            il.Emit(OpCodes.Callvirt, typeof(DataTable).GetMethod("get_Columns"));
            il.Emit(OpCodes.Stloc_1); //var columns = dr.Table.Columns

            List <PiMap> piMapList = DtoMappingHelper.GetPiMapList <T>();

            foreach (PiMap piMap in piMapList)
            {
                if (piMap.IsIgnore || piMap.Pi.SetMethod == null)
                {
                    continue;
                }
                //去掉关键词转义
                string fieldName = piMap.FieldName.Replace("\"", "").Replace("'", "").Replace("[", "").Replace("]", "");
                //加入判断条件 if (columns.Contains("Id") && !dataRow.IsNull("Id"))
                var endIfLabel = il.DefineLabel();
                il.Emit(OpCodes.Ldloc_1);             //columns
                il.Emit(OpCodes.Ldstr, fieldName);    //Id
                il.Emit(OpCodes.Callvirt, typeof(DataColumnCollection).GetMethod("Contains", new Type[] { typeof(string) }));
                il.Emit(OpCodes.Brfalse, endIfLabel); //判断columns.Contains("Id")

                il.Emit(OpCodes.Ldarg_0);             //dr
                il.Emit(OpCodes.Ldstr, fieldName);    //Id
                il.Emit(OpCodes.Callvirt, typeof(DataRow).GetMethod("IsNull", new Type[] { typeof(string) }));
                il.Emit(OpCodes.Brtrue, endIfLabel);  //判断dr.IsNull("Id")

                //自DataReader中读取值 调用get_Item方法 dataRow["Id"]
                il.Emit(OpCodes.Ldloc, result);    //result
                il.Emit(OpCodes.Ldarg_0);          //dataRow
                il.Emit(OpCodes.Ldstr, fieldName); //Id
                il.Emit(OpCodes.Callvirt, typeof(DataRow).GetMethod("get_Item", new Type[] { typeof(string) }));

                Type type = piMap.PropertyType; //拆箱
                if (type.IsValueType)
                {                               //直接拆箱 可空类型,也可以直接拆箱
                    if (piMap.IsEnum)
                    {
                        if (type.GenericTypeArguments.Length > 0)
                        {   //如果为可空枚举类型,抛出异常
                            Type realType = type.GenericTypeArguments[0];
                            throw new Exception($"对象{typeof(T).Name}属性{piMap.Pi.Name}[{realType.Name}]转换异常.不支持可空枚举类型.");
                        }
                    }
                    il.Emit(OpCodes.Unbox_Any, type);
                }
                else
                {   //引用类型
                    il.Emit(OpCodes.Castclass, type);
                }
                //给该属性设置对应值
                il.Emit(OpCodes.Callvirt, piMap.Pi.GetSetMethod());
                il.MarkLabel(endIfLabel);
            }
            /*给本地变量(result)返回值*/
            il.Emit(OpCodes.Ldloc, result);
            il.Emit(OpCodes.Ret);
        }