Guest User

Untitled

a guest
Apr 13th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Linq.Expressions;
  3.  
  4. namespace ExpressionTreeTest
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var result = Expression.Parameter(typeof(int), "result");
  11.  
  12.             var label = Expression.Label(typeof(int));
  13.  
  14.             var writeStringInfo = typeof(Console).GetMethod("WriteLine", new [] { typeof(int) });
  15.  
  16.             var block = Expression.Block(
  17.                 new[] { result },
  18.                 Expression.Assign(result, Expression.Constant(0)),
  19.                 Expression.Loop(
  20.                     Expression.IfThenElse(
  21.                         Expression.LessThan(result, Expression.Constant(100)),
  22.                         Expression.Block(
  23.                             Expression.Call(null, writeStringInfo, result),
  24.                             Expression.PostIncrementAssign(result)
  25.                         ),
  26.                         Expression.Break(label, result)
  27.                     ),
  28.                     label
  29.                 )
  30.             );
  31.  
  32.             var action = Expression.Lambda<Action>(block).Compile();
  33.             action();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment