title: Creating method dynamically, and executing it
body: <p><strong>Background:</strong></p>
  
  <p>I want to define few <code>static</code> methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed after re-generating the IL code from the byte array. </p>
  
  <p><strong>My Attempt:</strong> (<a href="http://en.wikipedia.org/wiki/Proof_of_concept">POC</a>)</p>
  
  <pre><code>public static class Experiment
  {
      public static int Multiply(int a, int b)
      {
          Console.WriteLine("Arguments ({0}, {1})", a, b);
          return a * b;
      }
  }
  </code></pre>
  
  <p>And then I get the IL code of the method body, as:</p>
  
  <pre><code>BindingFlags flags = BindingFlags.Public | BindingFlags.Static;
  MethodInfo meth = typeof(Experiment).GetMethod("Multiply", flags);
  byte[] il = meth.GetMethodBody().GetILAsByteArray();
  </code></pre>
  
  <p>So far I didn't create anything dynamically. But I've IL code as byte array, and I want to create an assembly, then a module in it, then a type, then a method - all dynamically. When creating the method body of the dynamically created method, I use the IL code which I got using reflection in the above code.</p>
  
  <p>The code-generation code is as follows:</p>
  
  <pre><code>AppDomain domain = AppDomain.CurrentDomain;
  AssemblyName aname = new AssemblyName("MyDLL");
  AssemblyBuilder assemBuilder = domain.DefineDynamicAssembly(
                                                 aname, 
                                                 AssemblyBuilderAccess.Run);
  
  ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("MainModule");
  
  TypeBuilder tb = modBuilder.DefineType("MyType", 
                              TypeAttributes.Public | TypeAttributes.Class);
  
  MethodBuilder mb = tb.DefineMethod("MyMethod", 
       MethodAttributes.Static | MethodAttributes.Public, 
       CallingConventions.Standard,
       typeof(int),                          // Return type
       new[] { typeof(int), typeof(int) });  // Parameter types
  
  mb.DefineParameter(1, ParameterAttributes.None, "value1");  // Assign name 
  mb.DefineParameter(2, ParameterAttributes.None, "value2");  // Assign name 
  
  //using the IL code to generate the method body
  mb.CreateMethodBody(il, il.Count()); 
  
  Type realType = tb.CreateType();
  
  var meth = realType.GetMethod("MyMethod");
  try
  {
      object result = meth.Invoke(null, new object[] { 10, 9878 });
      Console.WriteLine(result);  //should print 98780 (i.e 10 * 9878)
  }
  catch (Exception e)
  {
      Console.WriteLine(e.ToString());
  }
  </code></pre>
  
  <p>But instead of printing <code>98780</code> on the output window, it throws an exception saying,</p>
  
  <blockquote>
    <p>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an     invocation. ---> System.TypeLoadException: Could not load type 'Invalid_Token.0x0100001E'   from assembly 'MyDLL, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.<br>
    &nbsp; &nbsp;&nbsp; at MyType.MyMethod(Int32 value1, Int32 value2)
       [...]</p>
  </blockquote>
  
  <p>Please help me figuring out the cause of the error, and how to fix it.</p>