Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Tell you how to compile the expression tree into an executable file.

Tell you how to compile the expression tree into an executable file.

Scheduled Pinned Locked Moved LINQ
linqcsharpdata-structuresfunctionaltutorial
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxelena74
    wrote on last edited by
    #1

    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!

    N M 2 Replies Last reply
    0
    • M maxelena74

      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!

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      maxelena74 wrote:

      compile it into an executable file!

      Please explain


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • M maxelena74

        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!

        M Offline
        M Offline
        maxelena74
        wrote on last edited by
        #3

        I created a tree of expressions. How to get from the executable code. Need just a compiler, rather than Interpreter.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups