Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Expression Visitor for increment/decrement.");
            Console.WriteLine();

            Expression <Func <int, int> > expressionAddition    = num => num + 1;
            Expression <Func <int, int> > expressionSubtraction = num => num - 1;

            var incDecExpVisitor = new IncDecExpressionVisitor();

            Expression incrementExpression = incDecExpVisitor.Modify(expressionAddition);
            Expression decrementExpression = incDecExpVisitor.Modify(expressionSubtraction);

            Console.WriteLine(expressionAddition);
            Console.WriteLine(incrementExpression);
            Console.WriteLine(expressionSubtraction);
            Console.WriteLine(decrementExpression);

            Console.WriteLine("\nExpression Visitor for replace arguments in lambda.");
            Console.WriteLine();

            Expression <Func <int, int, double> > replaceableExpression = (arg1, arg2) => (arg1 * arg2 + 1.73 * (arg1 / 2 + arg2 / 2));

            Console.WriteLine(replaceableExpression);

            var subs = new Dictionary <string, ConstantExpression>
            {
                { "arg1", Expression.Constant(100) },
                { "arg2", Expression.Constant(10) }
            };

            var replacer           = new ParametersToConstantsReplacer(subs);
            var replacedExpression = replacer.Modify(replaceableExpression);

            Console.WriteLine(replacedExpression);
            var compiledExpression = ((LambdaExpression)replacedExpression).Compile();

            Console.WriteLine($"Result: {(double)compiledExpression.DynamicInvoke()}");

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Expression Visitor for increment/decrement.");
            Console.WriteLine();

            // todo: feel free to add your code here
            Expression <Func <int, int, int> > sourceExpression = (a, b) => a + (a + 1) + (b + 2) + (b + 1) - (a - 1) - (b - 1) - b;
            var resultExpression = new IncDecExpressionVisitor().IncDecExpression(sourceExpression);

            Console.WriteLine(sourceExpression);
            Console.WriteLine(resultExpression);

            var dictionary = new Dictionary <string, object> {
                { "a", 1 }, { "b", 2 }
            };

            resultExpression = new ChangeParameterVisitor().ChangeParameter(sourceExpression, dictionary);

            Console.WriteLine(resultExpression);
            Console.ReadLine();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Expression Visitor for increment/decrement.");
            Console.WriteLine();

            // todo: feel free to add your code here

            Expression <Func <int, int> >            exp  = (a) => a + (a + 1) * (a + 5) * (a + 1) + a + (a - 1);
            Expression <Func <int, string, string> > exp2 = (a, Name) => a + (a - 1) * (a + 5) * (a + 1) + Name;

            var result = new IncDecExpressionVisitor().VisitAndConvert(exp, "");

            Console.WriteLine(exp + " " + exp.Compile().Invoke(3));
            Console.WriteLine(result + " " + result.Compile().Invoke(3));

            Console.WriteLine();
            Console.WriteLine();
            var replacedParamsExpression = (new ReplaceParamsExpressionVisitor(exp2, a => 3, Name => " Raccoons forever").ReplaceWithConstant());

            Console.WriteLine(replacedParamsExpression + " result: " + replacedParamsExpression.Compile().DynamicInvoke());

            Console.ReadLine();
        }