Exemple #1
0
        private void SetPropertyExpression(ExpressionInfo expressionInfo, string expressionTokenText)
        {
            PropertyExpression = ExpressionParserHelper.GetPropertyExpression(
                expressionInfo.Parameter, expressionInfo.ParameterSyntax, expressionTokenText);

            PropertyInfo = (PropertyInfo)this.PropertyExpression.Member;

            if (PropertyInfo.PropertyType == typeof(bool))
            {
                Expression = Expression.IsTrue(this.PropertyExpression);
            }
        }
Exemple #2
0
        public void Update(
            ExpressionInfo expressionInfo,
            string expressionTokenText)
        {
            if (ExpressionParserHelper.IsPropertyToken(expressionInfo.ParameterSyntax, expressionTokenText))
            {
                SetPropertyExpression(expressionInfo, expressionTokenText);

                return;
            }

            if (OperationExpression != null)
            {
                var constantValue = expressionTokenText;

                if (PropertyInfo.PropertyType == typeof(string))
                {
                    constantValue = constantValue.Replace("'", null);
                }

                var comparisonConstantValue =
                    TypeDescriptor
                    .GetConverter(PropertyInfo.PropertyType)
                    .ConvertFromString(constantValue);

                Expression =
                    OperationExpression(PropertyExpression, Expression.Constant(comparisonConstantValue));

                return;
            }

            if (ExpressionParserHelper.GetOperatorFunc(expressionTokenText) != null)
            {
                OperationExpression = ExpressionParserHelper.GetOperatorFunc(expressionTokenText);
            }
        }
Exemple #3
0
        public Expression <Func <T, bool> > Parse <T>(string evalExpression)
        {
            var expressionInfo =
                ExpressionInfo.Create <T>(evalExpression);

            var expressionBuilder =
                new ExpressionBuilder(expressionInfo);

            var expressionTokens    = new List <Expression>();
            var logicOperatorTokens = new List <string>();

            var token = TokenToSkip;

            while (!EndOfTokens(expressionInfo, token))
            {
                var expressionResult =
                    expressionBuilder.Parse(token);

                token += expressionResult.TokensParsed;

                expressionTokens.Add(expressionResult.Expression);

                if (EndOfTokens(expressionInfo, token))
                {
                    break;
                }

                var expressionText = expressionInfo.Tokens[token];

                if (ExpressionParserHelper.IsLogicToken(expressionText))
                {
                    logicOperatorTokens.Add(expressionText);
                }

                token++;
            }

            var logicOperatorIndex = 0;

            Expression expression = null;

            for (var index = 0; index < expressionTokens.Count; index++)
            {
                if (index == 0)
                {
                    continue;
                }

                var logicFunc =
                    ExpressionParserHelper
                    .GetLogicExpression(logicOperatorTokens[logicOperatorIndex]);

                expression =
                    logicFunc(expressionTokens[index - 1], expressionTokens[index]);

                logicOperatorIndex++;
            }

            if (expression == null)
            {
                expression = expressionTokens.Single();
            }

            return
                (Expression.Lambda <Func <T, bool> >(expression, expressionInfo.Parameter));
        }