Exemplo n.º 1
0
        private Expression GetExpression(string methodName, params NodeInfo[] nodes)
        {
            var expressions = nodes.Select(x => new { Expression = InnerBuild(x.Node), Type = _expressionTypeResolver.GetTypeFor(x.Node, _type), x.ReturnIfNull });

            var nullExpression = Expression.Constant(null);

            Expression returnNullTest = Expression.Constant(false);

            returnNullTest = expressions.Where(x => x.ReturnIfNull).Select(x => x.Expression).Aggregate(returnNullTest, (current, exp) => Expression.Or(current, Expression.ReferenceEqual(Expression.Convert(exp, typeof(object)), nullExpression)));

            var method = CSharpExpressionHelperMethodResolver.GetMethod(methodName, expressions.Select(x => x.Type).ToArray());

            if (method == null)
            {
                return(null);
            }

            var parameterTypes = method.GetParameters().Select(x => x.ParameterType).ToArray();

            var arguments = expressions.Select((x, i) => GetArgument(x.Expression, parameterTypes[i])).ToArray();

            var methodCall = Expression.Convert(Expression.Call(method, arguments), typeof(object));

            return(Expression.Condition(returnNullTest, nullExpression, methodCall));
        }
        private Type Build(FunctionNode node)
        {
            var functionName = node.FunctionName.ToLower();

            switch (functionName)
            {
            case "abs":
                var parameterTypes = node.Arguments.Select(InnerBuild).ToArray();
                var method         = CSharpExpressionHelperMethodResolver.GetMethod("Abs", parameterTypes);

                return(method.ReturnType);

            case "ceiling":
            case "floor":
                return(typeof(decimal));

            case "power":
                return(typeof(double));

            case "substring":
            case "left":
            case "right":
            case "replace":
                return(typeof(string));

            case "contains":
                return(typeof(bool));

            case "len":
                return(typeof(int));

            default:
                return(null);
            }
        }
        private Type Build(MaxFunctionNode node)
        {
            var parameterTypes = node.Arguments.Select(InnerBuild).ToArray();
            var method         = CSharpExpressionHelperMethodResolver.GetMethod("Max", parameterTypes);

            return(method.ReturnType);
        }
        private Type Build(IifFunctionNode node)
        {
            var ifTrueType  = InnerBuild(node.IfTrue);
            var ifFalseType = InnerBuild(node.IfFalse);

            var method = CSharpExpressionHelperMethodResolver.GetMethod("Iif", typeof(bool), ifTrueType, ifFalseType);

            return(GetUnderlyingType(method.ReturnType));
        }
        private Type Build(BinaryExpressionNode node)
        {
            string methodName;

            if (!_methodNames.TryGetValue(node.Op, out methodName))
            {
                return(null);
            }

            var leftType  = InnerBuild(node.Left);
            var rightType = InnerBuild(node.Right);
            var method    = CSharpExpressionHelperMethodResolver.GetMethod(methodName, leftType, rightType);

            return(method.ReturnType);
        }