Exemplo n.º 1
0
        private static Expression CreateAnyAllExpression(
            Expression left,
            Expression right,
            ParameterExpression sourceParameter,
            IEnumerable <ParameterExpression> lambdaParameters,
            MethodInfo anyAllMethod)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            var genericFunc = typeof(Func <,>)
                              .MakeGenericType(
                MethodProvider.GetIEnumerableImpl(left.Type).GetGenericArguments()[0],
                typeof(bool));

            var filteredParameters = new ParameterVisitor()
                                     .GetParameters(right)
                                     .Where(p => p.Name != sourceParameter.Name)
                                     .ToArray();

            if (filteredParameters.Length > 0)
            {
                return(Expression.Call(
                           anyAllMethod,
                           left,
                           Expression.Lambda(genericFunc, right, filteredParameters)));
            }

            return(Expression.Call(
                       MethodProvider.GetAnyAllMethod("All", left.Type),
                       left,
                       Expression.Lambda(genericFunc, right, lambdaParameters)));
        }
Exemplo n.º 2
0
        private static Expression GetFunction(string function, Expression left, Expression right,
                                              ParameterExpression sourceParameter,
                                              IEnumerable <ParameterExpression> lambdaParameters)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }

            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            switch (function.ToLowerInvariant())
            {
            case "substringof":
                return(Expression.Call(right, MethodProvider.ContainsMethod, new[] { left }));

            case "endswith":
                return(Expression.Call(left, MethodProvider.EndsWithMethod, new[] { right, MethodProvider.IgnoreCaseExpression }));

            case "startswith":
                return(Expression.Call(left, MethodProvider.StartsWithMethod,
                                       new[] { right, MethodProvider.IgnoreCaseExpression }));

            case "length":
                return(Expression.Property(left, MethodProvider.LengthProperty));

            case "indexof":
                return(Expression.Call(left, MethodProvider.IndexOfMethod, new[] { right, MethodProvider.IgnoreCaseExpression }));

            case "substring":
                return(Expression.Call(left, MethodProvider.SubstringMethod, new[] { right }));

            case "tolower":
                return(Expression.Call(left, MethodProvider.ToLowerMethod));

            case "toupper":
                return(Expression.Call(left, MethodProvider.ToUpperMethod));

            case "trim":
                return(Expression.Call(left, MethodProvider.TrimMethod));

            case "hour":
                return(Expression.Property(left, MethodProvider.HourProperty));

            case "minute":
                return(Expression.Property(left, MethodProvider.MinuteProperty));

            case "second":
                return(Expression.Property(left, MethodProvider.SecondProperty));

            case "day":
                return(Expression.Property(left, MethodProvider.DayProperty));

            case "month":
                return(Expression.Property(left, MethodProvider.MonthProperty));

            case "year":
                return(Expression.Property(left, MethodProvider.YearProperty));

            case "round":
                return
                    (Expression.Call(
                         left.Type == typeof(double) ? MethodProvider.DoubleRoundMethod : MethodProvider.DecimalRoundMethod, left));

            case "floor":
                return
                    (Expression.Call(
                         left.Type == typeof(double) ? MethodProvider.DoubleFloorMethod : MethodProvider.DecimalFloorMethod, left));

            case "ceiling":
                return
                    (Expression.Call(
                         left.Type == typeof(double) ? MethodProvider.DoubleCeilingMethod : MethodProvider.DecimalCeilingMethod,
                         left));

            case "any":
            case "all":
            {
                return(CreateAnyAllExpression(
                           left,
                           right,
                           sourceParameter,
                           lambdaParameters,
                           MethodProvider.GetAnyAllMethod(function.Capitalize(), left.Type)));
            }

            default:
                return(null);
            }
        }
        private static Expression GetFunction(string function, Expression left, Expression right, ParameterExpression sourceParameter, ICollection <ParameterExpression> lambdaParameters)
        {
            CustomContract.Requires(function != null);
            CustomContract.Requires(left != null);

            switch (function.ToLowerInvariant())
            {
            case "substringof":
                return(Expression.Call(right, MethodProvider.ContainsMethod, new[] { left }));

            case "endswith":
                return(Expression.Call(left, MethodProvider.EndsWithMethod, new[] { right }));

            case "startswith":
                return(Expression.Call(left, MethodProvider.StartsWithMethod, new[] { right }));

            case "length":
                return(Expression.Property(left, MethodProvider.LengthProperty));

            case "indexof":
                return(Expression.Call(left, MethodProvider.IndexOfMethod, new[] { right }));

            case "substring":
                return(Expression.Call(left, MethodProvider.SubstringMethod, new[] { right }));

            case "tolower":
                return(Expression.Call(left, MethodProvider.ToLowerMethod));

            case "toupper":
                return(Expression.Call(left, MethodProvider.ToUpperMethod));

            case "trim":
                return(Expression.Call(left, MethodProvider.TrimMethod));

            case "hour":
                return(Expression.Property(left, MethodProvider.HourProperty));

            case "minute":
                return(Expression.Property(left, MethodProvider.MinuteProperty));

            case "second":
                return(Expression.Property(left, MethodProvider.SecondProperty));

            case "day":
                return(Expression.Property(left, MethodProvider.DayProperty));

            case "month":
                return(Expression.Property(left, MethodProvider.MonthProperty));

            case "year":
                return(Expression.Property(left, MethodProvider.YearProperty));

            case "round":
                return(Expression.Call(left.Type == typeof(double) ? MethodProvider.DoubleRoundMethod : MethodProvider.DecimalRoundMethod, left));

            case "floor":
                return(Expression.Call(left.Type == typeof(double) ? MethodProvider.DoubleFloorMethod : MethodProvider.DecimalFloorMethod, left));

            case "ceiling":
                return(Expression.Call(left.Type == typeof(double) ? MethodProvider.DoubleCeilingMethod : MethodProvider.DecimalCeilingMethod, left));

            case "any":
            case "all":
            {
                CustomContract.Assume(right != null);
                CustomContract.Assume(!string.IsNullOrEmpty(function));

                return(CreateAnyAllExpression(
                           left,
                           right,
                           sourceParameter,
                           lambdaParameters,
                           MethodProvider.GetAnyAllMethod(function.Capitalize(), left.Type)));
            }

            default:
                return(null);
            }
        }