public NumericListElementExpression(IObjectExpression listExpr, INumericExpression indexExpr)
        {
            this.listExpr  = listExpr;
            this.indexExpr = indexExpr;

            Type objectType = listExpr.ObjectType;
            Type itemType;

            if (objectType.IsArray)
            {
                itemType = objectType.GetElementType();
            }
            else if (ReflectionHelper.IsList(objectType))
            {
                itemType = objectType.GetGenericArguments()[0];
            }
            else
            {
                throw new NotSupportedException();
            }

            MethodInfo generateEvaluatorMethod = GetType()
                                                 .GetMethod("GenerateEvaluator", BindingFlags.NonPublic | BindingFlags.Static)
                                                 .MakeGenericMethod(itemType);

            Func <Func <object, int, double> > generateEvaluatorDelegate =
                ReflectionHelper.CreateDelegate <Func <object, int, double> >(generateEvaluatorMethod);

            evaluator = generateEvaluatorDelegate();
        }
Пример #2
0
        public ObjectMethodExpression(IObjectExpression containingObject, MethodInfo methodInfo, ListExpression argumentsExpr)
        {
            this.containingObject = containingObject;
            this.methodInfo       = methodInfo;
            this.argumentsExpr    = argumentsExpr;

            Type containingObjectType = containingObject.ObjectType;

            ObjectType = methodInfo.ReturnType;
            MethodInfo generateEvaluatorMethod;

            ParameterInfo[] parameters = methodInfo.GetParameters();

            switch (parameters.Length)
            {
            case 0:
                generateEvaluatorMethod = GetType()
                                          .GetMethod("GenerateFunctor0", BindingFlags.NonPublic | BindingFlags.Static)
                                          .MakeGenericMethod(containingObjectType, ObjectType);
                break;

            case 1:
                generateEvaluatorMethod = GetType()
                                          .GetMethod("GenerateFunctor1", BindingFlags.NonPublic | BindingFlags.Static)
                                          .MakeGenericMethod(containingObjectType, parameters[0].ParameterType, ObjectType);
                break;

            case 2:
                generateEvaluatorMethod = GetType()
                                          .GetMethod("GenerateFunctor2", BindingFlags.NonPublic | BindingFlags.Static)
                                          .MakeGenericMethod(containingObjectType,
                                                             parameters[0].ParameterType,
                                                             parameters[1].ParameterType,
                                                             ObjectType);
                break;

            case 3:
                generateEvaluatorMethod = GetType()
                                          .GetMethod("GenerateFunctor3", BindingFlags.NonPublic | BindingFlags.Static)
                                          .MakeGenericMethod(containingObjectType,
                                                             parameters[0].ParameterType,
                                                             parameters[1].ParameterType,
                                                             parameters[2].ParameterType,
                                                             ObjectType);
                break;

            default:
                throw new NotSupportedException();
            }

            var generateEvaluatorDelegate = ReflectionHelper
                                            .CreateDelegate <MethodInfo, ListExpression, Func <Dictionary <string, object>, object, object> >(generateEvaluatorMethod);

            evaluator = generateEvaluatorDelegate(methodInfo, argumentsExpr);
        }
        public NumericPropertyExpression(IObjectExpression containingObject, PropertyInfo propertyInfo)
        {
            this.containingObject = containingObject;
            this.propertyInfo     = propertyInfo;

            Type containingObjectType = containingObject.ObjectType;
            Type propertyType         = propertyInfo.PropertyType;

            MethodInfo generateEvaluatorMethod = GetType()
                                                 .GetMethod("GenerateEvaluator", BindingFlags.NonPublic | BindingFlags.Static)
                                                 .MakeGenericMethod(containingObjectType, propertyType);

            Func <PropertyInfo, Func <object, double> > generateEvaluatorDelegate =
                ReflectionHelper.CreateDelegate <PropertyInfo, Func <object, double> >(generateEvaluatorMethod);

            evaluator = generateEvaluatorDelegate(propertyInfo);
        }
        private IExpression VariableExpression()
        {
            IExpression expr = ElementExpression(VariableOrFunctionExpression());

            while (lexer.Accept(TokenType.Dot))
            {
                IObjectExpression containingObjectExpr = expr as IObjectExpression;

                if (containingObjectExpr == null)
                {
                    throw BuildException("Cannot access property or method on expression {0}", expr);
                }

                expr = ElementExpression(MemberExpression(containingObjectExpr));
            }

            return(expr);
        }
Пример #5
0
        public Func <Dictionary <string, object>, T> GetItemGetter <T>(int index)
        {
            IExpression itemExpr = Expressions[index];

            Type itemType = typeof(T);

            if (ReflectionHelper.IsNumber(itemType))
            {
                INumericExpression numericExpr = (INumericExpression)itemExpr;

                Func <double, T> converter = ReflectionHelper.GenerateFromDoubleConverter <T>();
                return(variables => converter(numericExpr.Evaluate(variables)));
            }
            else if (itemType == typeof(bool))
            {
                MethodInfo generateGetterMethod = GetType()
                                                  .GetMethod("GetBooleanItemGetter", BindingFlags.NonPublic | BindingFlags.Static);

                Func <IBooleanExpression, Func <Dictionary <string, object>, T> > generateGetterDelegate =
                    ReflectionHelper.CreateDelegate <IBooleanExpression, Func <Dictionary <string, object>, T> >(generateGetterMethod);

                IBooleanExpression booleanExpr = (IBooleanExpression)itemExpr;
                return(generateGetterDelegate(booleanExpr));
            }
            else if (itemType == typeof(string))
            {
                MethodInfo generateGetterMethod = GetType()
                                                  .GetMethod("GetStringItemGetter", BindingFlags.NonPublic | BindingFlags.Static);

                Func <IStringExpression, Func <Dictionary <string, object>, T> > generateGetterDelegate =
                    ReflectionHelper.CreateDelegate <IStringExpression, Func <Dictionary <string, object>, T> >(generateGetterMethod);

                IStringExpression booleanExpr = (IStringExpression)itemExpr;
                return(generateGetterDelegate(booleanExpr));
            }
            else
            {
                IObjectExpression objectExpr = (IObjectExpression)itemExpr;

                return(variables => (T)objectExpr.GetInstance(variables));
            }
        }
        private IExpression MemberExpression(IObjectExpression containingObjectExpr)
        {
            string identifier = lexer.Identifier();

            // method
            if (lexer.Peek(TokenType.OpenParenthesis))
            {
                ListExpression argsExpr = (ListExpression)InvocationExpression();

                MethodInfo methodInfo = containingObjectExpr.ObjectType.GetMethod(identifier);
                Type       returnType = methodInfo.ReturnType;

                if (ReflectionHelper.IsNumber(returnType))
                {
                    return(new NumericMethodExpression(containingObjectExpr, methodInfo, argsExpr));
                }
                else
                {
                    return(new ObjectMethodExpression(containingObjectExpr, methodInfo, argsExpr));
                }
            }
            // property
            else
            {
                PropertyInfo propertyInfo = containingObjectExpr.ObjectType.GetProperty(identifier);
                Type         propertyType = propertyInfo.PropertyType;

                if (ReflectionHelper.IsNumber(propertyType))
                {
                    return(new NumericPropertyExpression(containingObjectExpr, propertyInfo));
                }
                else
                {
                    return(new ObjectPropertyExpression(containingObjectExpr, propertyInfo));
                }
            }
        }
        private IExpression ElementExpression(IExpression expr)
        {
            if (lexer.Peek(TokenType.OpenBracket))
            {
                INumericExpression indexExpr = (INumericExpression)BracketExpression();
                IObjectExpression  listExpr  = expr as IObjectExpression;

                if (listExpr == null)
                {
                    throw BuildException("Cannot apply indexing with [] on expression", expr);
                }

                if (ReflectionHelper.IsNumberList(listExpr.ObjectType))
                {
                    expr = new NumericListElementExpression(listExpr, indexExpr);
                }
                else
                {
                    expr = new ObjectListElementExpression(listExpr, indexExpr);
                }
            }

            return(expr);
        }