Example #1
0
        public override Expression BuildExpression(params ParameterExpression[] parameters)
        {
            if (parameters == null || parameters.Length != 1 || parameters[0].Type != typeof(T))
            {
                throw new RuleEngineException($"{nameof(BuildExpression)} must call with one parameter of {typeof(T)}");
            }

            var conditionalExpression = ConditionRule.BuildExpression(parameters);

            if (!(conditionalExpression is LambdaExpression))
            {
                conditionalExpression = Expression.Lambda(conditionalExpression, parameters);
            }

            var trueExpression = TrueRule.BuildExpression(parameters);

            if (!(trueExpression is LambdaExpression))
            {
                trueExpression = Expression.Lambda(trueExpression, parameters);
            }

            var falseExpression = FalseRule.BuildExpression(parameters);

            if (!(falseExpression is LambdaExpression))
            {
                falseExpression = Expression.Lambda(falseExpression, parameters);
            }

            ExpressionForThisRule = Expression.Condition(Expression.Invoke(conditionalExpression, parameters.Cast <Expression>()),
                                                         Expression.Invoke(trueExpression, parameters.Cast <Expression>()),
                                                         Expression.Invoke(falseExpression, parameters.Cast <Expression>()));
            return(ExpressionForThisRule);
        }
Example #2
0
        public override Expression BuildExpression(params ParameterExpression[] parameters)
        {
            if (parameters == null || parameters.Length != 1 || parameters[0].Type != typeof(T1))
            {
                throw new RuleEngineException($"{nameof(BuildExpression)} must call with one parameter of {typeof(T1)}");
            }

            var returnLabel = Expression.Label(typeof(T2), "returnLabel");

            var conditionalExpression = ConditionRule.BuildExpression(parameters);

            if (!(conditionalExpression is LambdaExpression))
            {
                conditionalExpression = Expression.Lambda(conditionalExpression, parameters);
            }

            var trueExpression = TrueRule.BuildExpression(parameters);

            if (!(trueExpression is LambdaExpression))
            {
                trueExpression = Expression.Lambda(trueExpression, parameters);
            }

            var falseExpression = FalseRule.BuildExpression(parameters);

            if (!(falseExpression is LambdaExpression))
            {
                falseExpression = Expression.Lambda(falseExpression, parameters);
            }

            var ifThenElseExpression = Expression.IfThenElse(
                Expression.Invoke(conditionalExpression, parameters.Cast <Expression>()),
                Expression.Return(returnLabel, Expression.Invoke(trueExpression, parameters.Cast <Expression>())),
                Expression.Return(returnLabel, Expression.Invoke(falseExpression, parameters.Cast <Expression>()))
                );

            Debug.WriteLine($"ConditionRule:{Environment.NewLine}{ConditionRule.ExpressionDebugView()}");
            Debug.WriteLine($"TrueRule:{Environment.NewLine}{TrueRule.ExpressionDebugView()}");
            Debug.WriteLine($"FalseRule:{Environment.NewLine}{FalseRule.ExpressionDebugView()}");

            var defaultForReturnLabel = Expression.Convert(Expression.Constant(default(T2)), typeof(T2));

            ExpressionForThisRule = Expression.Block(ifThenElseExpression,
                                                     Expression.Label(returnLabel, defaultForReturnLabel));
            return(ExpressionForThisRule);
        }