Tell you how to compile the expression tree into an executable file.
-
Tell you how to compile the expression tree into an executable file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;namespace ConsoleApplication1
{class Program { static void Main() { // Creating a parameter expression. ParameterExpression value = Expression.Parameter(typeof(int), "value"); // Creating an expression to hold a local variable. ParameterExpression result = Expression.Parameter(typeof(int), "result"); // Creating a label to jump to from a loop. LabelTarget label = Expression.Label(typeof(int)); // Creating a method body. BlockExpression block = Expression.Block( // Adding a local variable. new\[\] { result }, // Assigning a constant to a local variable: result = 1 Expression.Assign(result, Expression.Constant(1)), // Adding a loop. Expression.Loop( // Adding a conditional block into the loop. Expression.IfThenElse( // Condition: value > 1 Expression.GreaterThan(value, Expression.Constant(1)), // If true: result \*= value -- Expression.MultiplyAssign(result, Expression.PostDecrementAssign(value)), // If false, exit the loop and go to the label. Expression.Break(label, result) ), // Label to jump to. label ) ); // Compile and execute an expression tree. int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5); Console.WriteLine(factorial); // Prints 120. } }
}
Something like that, but simply create an expression tree and compile it into an executable file!
-
Tell you how to compile the expression tree into an executable file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;namespace ConsoleApplication1
{class Program { static void Main() { // Creating a parameter expression. ParameterExpression value = Expression.Parameter(typeof(int), "value"); // Creating an expression to hold a local variable. ParameterExpression result = Expression.Parameter(typeof(int), "result"); // Creating a label to jump to from a loop. LabelTarget label = Expression.Label(typeof(int)); // Creating a method body. BlockExpression block = Expression.Block( // Adding a local variable. new\[\] { result }, // Assigning a constant to a local variable: result = 1 Expression.Assign(result, Expression.Constant(1)), // Adding a loop. Expression.Loop( // Adding a conditional block into the loop. Expression.IfThenElse( // Condition: value > 1 Expression.GreaterThan(value, Expression.Constant(1)), // If true: result \*= value -- Expression.MultiplyAssign(result, Expression.PostDecrementAssign(value)), // If false, exit the loop and go to the label. Expression.Break(label, result) ), // Label to jump to. label ) ); // Compile and execute an expression tree. int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5); Console.WriteLine(factorial); // Prints 120. } }
}
Something like that, but simply create an expression tree and compile it into an executable file!
maxelena74 wrote:
compile it into an executable file!
Please explain
I know the language. I've read a book. - _Madmatt
-
Tell you how to compile the expression tree into an executable file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;namespace ConsoleApplication1
{class Program { static void Main() { // Creating a parameter expression. ParameterExpression value = Expression.Parameter(typeof(int), "value"); // Creating an expression to hold a local variable. ParameterExpression result = Expression.Parameter(typeof(int), "result"); // Creating a label to jump to from a loop. LabelTarget label = Expression.Label(typeof(int)); // Creating a method body. BlockExpression block = Expression.Block( // Adding a local variable. new\[\] { result }, // Assigning a constant to a local variable: result = 1 Expression.Assign(result, Expression.Constant(1)), // Adding a loop. Expression.Loop( // Adding a conditional block into the loop. Expression.IfThenElse( // Condition: value > 1 Expression.GreaterThan(value, Expression.Constant(1)), // If true: result \*= value -- Expression.MultiplyAssign(result, Expression.PostDecrementAssign(value)), // If false, exit the loop and go to the label. Expression.Break(label, result) ), // Label to jump to. label ) ); // Compile and execute an expression tree. int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5); Console.WriteLine(factorial); // Prints 120. } }
}
Something like that, but simply create an expression tree and compile it into an executable file!
I created a tree of expressions. How to get from the executable code. Need just a compiler, rather than Interpreter.