Ejemplo n.º 1
0
        /* We can use the MultiplyToAdd class to modify the expression created in previous example
         * and create a new method that doubles any parameter given to it. */
        private static void ModifyingAnExpressionTreeExample()
        {
            //build the expression tree: Expression<Func<int,int>> square = num => num * num;

            //The parameter for the expression is an integer
            ParameterExpression numParam = Expression.Parameter(typeof(int), "num");

            //The operation to be performed is to square the parameter
            BinaryExpression squareOperation = Expression.Multiply(numParam, numParam);

            //This creates an expression tree that describes the square operation
            Expression <Func <int, int> > square = Expression.Lambda <Func <int, int> >(
                squareOperation,
                new ParameterExpression[] { numParam });

            //Compile the tree to make an executable method and assign it to a delegate
            Func <int, int> doSquare = square.Compile();

            //Call the delegate
            Console.WriteLine("Square of 2: {0}", doSquare(2));

            //Modify the expression to replace the multiply with an add
            MultiplyToAdd m = new MultiplyToAdd();
            Expression <Func <int, int> > addExpression = (Expression <Func <int, int> >)m.Modify(square);
            Func <int, int> doAdd = addExpression.Compile();

            Console.WriteLine("Double of 4: {0}", doAdd(4));
        }
        public static void exectuteTest()
        {
            ParameterExpression numParam = Expression.Parameter(typeof(int), "num");

            BinaryExpression squareOption = Expression.Multiply(numParam, numParam);

            Expression <Func <int, int> > square = Expression.Lambda <Func <int, int> >(squareOption, new ParameterExpression[] { numParam });

            Func <int, int> doSquare = square.Compile();

            Console.WriteLine($"Result square {doSquare(2)}");

            Console.WriteLine();

            Console.ReadKey();

            MultiplyToAdd m = new MultiplyToAdd();

            Expression <Func <int, int> > addExpression = (Expression <Func <int, int> >)m.Modify(square);

            Func <int, int> doAdd = addExpression.Compile();

            Console.WriteLine($"Result add {doAdd(4)}");

            Console.WriteLine();



            Console.ReadKey();
        }
Ejemplo n.º 3
0
        // An	expression	tree	is	immutable,	which	means	that	the	elements	in	the expression	cannot	be	changed	once	the	expression
        // has	been	created.	To	modify an	expression	tree	you	must	make	a	copy	of	the	tree	which	contains	the	modified
        // behaviors.
        public void ModifyingExpressionTree()
        {
            Expression <Func <int, int> > squareExp = num => num * num;
            MultiplyToAdd m = new MultiplyToAdd();
            Expression <Func <int, int> > addExpression = (Expression <Func <int, int> >)m.Modify(squareExp);
            Func <int, int> doAdd = addExpression.Compile();

            Console.WriteLine("Double	of	4:	{0}", doAdd(4));
        }