Advertisement
simonradev

LoopCreatedWithIL

Oct 15th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System;
  2. using System.Reflection.Emit;
  3.  
  4. namespace ConsoleApp2
  5. {
  6.  
  7.     class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             int invokationOfHandMadeMethodResult = Calculate(10);
  12.             Console.WriteLine(invokationOfHandMadeMethodResult);
  13.  
  14.             int invokationOfDynamicallyCreatedMethodResult = CreateCalculateMethod()(10);
  15.             Console.WriteLine(invokationOfDynamicallyCreatedMethodResult);
  16.         }  
  17.  
  18.         public static Func<int, int> CreateCalculateMethod()
  19.         {
  20.             Type integerType = typeof(int);
  21.             DynamicMethod dynamicMethod = new DynamicMethod("Calculate", integerType, new[] { integerType });
  22.  
  23.             //----------------------------------------------------------------
  24.  
  25.             ILGenerator il = dynamicMethod.GetILGenerator();
  26.  
  27.             Label loopStartLable = il.DefineLabel();
  28.             Label methodReturnResultLable = il.DefineLabel();
  29.  
  30.             //-- Local variable [0] -> int result
  31.             il.DeclareLocal(integerType);
  32.             il.Emit(OpCodes.Ldc_I4_0);
  33.             il.Emit(OpCodes.Stloc_0);
  34.  
  35.             //-- Local variable [1] -> int i
  36.             il.DeclareLocal(integerType);
  37.             il.Emit(OpCodes.Ldc_I4_0);
  38.             il.Emit(OpCodes.Stloc_1);
  39.  
  40.             //-- Loop start check -> i < 10
  41.             il.MarkLabel(loopStartLable);
  42.             il.Emit(OpCodes.Ldloc_1);
  43.             il.Emit(OpCodes.Ldc_I4, 10);
  44.             il.Emit(OpCodes.Bge, methodReturnResultLable);
  45.  
  46.             //-- Loop body -> result += i * x (The next THREE steps)
  47.  
  48.             //-- 1. i * x
  49.             il.Emit(OpCodes.Ldloc_1);
  50.             il.Emit(OpCodes.Ldarg_0);
  51.             il.Emit(OpCodes.Mul);
  52.  
  53.             //-- 2. result + (i * x)
  54.             il.Emit(OpCodes.Ldloc_0);
  55.             il.Emit(OpCodes.Add);
  56.  
  57.             //-- 3. result = result + (i * x)
  58.             il.Emit(OpCodes.Stloc_0);
  59.  
  60.             //-- i++
  61.             il.Emit(OpCodes.Ldloc_1);
  62.             il.Emit(OpCodes.Ldc_I4_1);
  63.             il.Emit(OpCodes.Add);
  64.             il.Emit(OpCodes.Stloc_1);
  65.  
  66.             //-- Branch/Go to the beginning of the loop and repeat
  67.             il.Emit(OpCodes.Br, loopStartLable);
  68.  
  69.             //-- End of method -> return result
  70.             il.MarkLabel(methodReturnResultLable);
  71.             il.Emit(OpCodes.Ldloc_0);
  72.             il.Emit(OpCodes.Ret);
  73.  
  74.             //----------------------------------------------------------------
  75.  
  76.             Func<int, int> method = (Func<int, int>)dynamicMethod.CreateDelegate(typeof(Func<int, int>));
  77.  
  78.             return method;
  79.         }
  80.  
  81.         public static int Calculate(int x)
  82.         {
  83.             int result = 0;
  84.             for (int i = 0; i < 10; i++)
  85.             {
  86.                 result += i * x;
  87.             }
  88.  
  89.             return result;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement