Esempio n. 1
0
        public Expression GetExpression(Expression parm, Type TableType)
        {
            Expression Leftex = null;

            if (Left is ExpressAttrInterface)
            {
                ExpressAttrInterface AInterface = Left as ExpressAttrInterface;
                Leftex = AInterface.GetExpression(parm, TableType);
            }
            else if (Left is string)
            {
                Leftex = Expression.Property(parm, TableType.GetProperty(Left.ToString()));
            }
            else
            {
                if (Left == null || BaseToolClass.GetObType(TableType.GetProperty(Left.ToString()).PropertyType) == BaseToolClass.GetObType(Left.GetType()))
                {
                    Leftex = Expression.Constant(Left, TableType.GetProperty(Left.ToString()).PropertyType);
                }
                else
                {
                    Leftex = Expression.Constant(Left, Left.GetType());
                }
            }
            Expression Rightex = null;

            //object RightAttr = Right.GetType().GetCustomAttributes(typeof(ExpressAttrInterface), true).FirstOrDefault();
            if (Right is ExpressAttrInterface)
            {
                ExpressAttrInterface AInterface = Right as ExpressAttrInterface;
                Rightex = AInterface.GetExpression(parm, TableType);
            }
            else if (Right is string)
            {
                Rightex = Expression.Property(parm, TableType.GetProperty(Right.ToString()));
            }
            else
            {
                if (Left == null || BaseToolClass.GetObType(TableType.GetProperty(Right.ToString()).PropertyType) == BaseToolClass.GetObType(Right.GetType()))
                {
                    Rightex = Expression.Constant(Right, TableType.GetProperty(Right.ToString()).PropertyType);
                }
                else
                {
                    Rightex = Expression.Constant(Right, Right.GetType());
                }
            }
            switch (op)
            {
            case Operation.加:
                return(Expression.Add(Leftex, Rightex));

            case Operation.减:
                return(Expression.Subtract(Leftex, Rightex));

            case Operation.乘:
                return(Expression.Multiply(Leftex, Rightex));

            case Operation.除:
                return(Expression.Divide(Leftex, Rightex));

            case Operation.余:
                return(Expression.Modulo(Leftex, Rightex));

            case Operation.反:
                return(Expression.Negate(Leftex));

            default:
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 规则条件
        /// </summary>
        /// <param name="symbl"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="Selvalue"></param>
        /// <returns></returns>
        public static Expression whereSymbol(Symbol symbl, Expression left, Expression right = null, object Selvalue = null)
        {
            Symbol     vlsymbl = symbl;
            Expression where2  = null;


            if (right == null)
            {
                if (Symbol.空 == vlsymbl)
                {
                    bool IsNull = (bool)Selvalue;

                    right = Expression.Constant(null, left.Type);
                    if (IsNull)
                    {
                        vlsymbl = Symbol.等于;
                    }
                    else
                    {
                        vlsymbl = Symbol.等于;
                    }
                }
                else
                {
                    if (Selvalue == null || BaseToolClass.GetObType(left.Type) == BaseToolClass.GetObType(Selvalue.GetType()))
                    {
                        right = Expression.Constant(Selvalue, left.Type);
                    }
                    else
                    {
                        right = Expression.Constant(Selvalue, Selvalue.GetType());
                    }
                }
            }
            switch (vlsymbl)
            {
            case Symbol.等于:
                where2 = Expression.Equal(left, right);
                break;

            case Symbol.大于等于:
                where2 = Expression.GreaterThanOrEqual(left, right);
                break;

            case Symbol.大于:
                where2 = Expression.GreaterThan(left, right);
                break;

            case Symbol.小于等于:
                where2 = Expression.LessThanOrEqual(left, right);
                break;

            case Symbol.小于:
                where2 = Expression.LessThan(left, right);
                break;

            case Symbol.相似:
                where2 = Expression.Call(
                    left,
                    typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
                    right);
                break;

            case Symbol.包含:
                if (Selvalue.GetType().IsArray)
                {
                    where2 = Expression.Call(
                        null,
                        BaseToolClass.GetMethodInfo(typeof(Enumerable), "Contains", null, new Type[] { typeof(IEnumerable <TSource_1>), typeof(TSource_1) }, true).MakeGenericMethod(Selvalue.GetType().GetElementType()),
                        right,
                        left);
                }
                else
                {
                    where2 = Expression.Call(
                        right,
                        Selvalue.GetType().GetMethod("Contains", new Type[] { Selvalue.GetType() }),
                        left);
                };
                break;

            case Symbol.等于:
                where2 = Expression.NotEqual(left, right);
                break;
            }
            return(where2);
        }