title: Create DynamicMethod from Action<T> instructions
body: <p>I am playing around with DynamicMethod and aim to do the following:</p>
  
  <p>I have an Action from which I obtain the IL code as bytes using <code>GetILAsByteArray()</code>. From this bytes I would like to create a Dynamic method and execute is. Here an example of what I am trying to do:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          //Create action and execute
          Action&lt;string&gt; myAction = s =&gt;
          {
              Console.WriteLine("Hello " + s);
          };
          myAction("World");
          //Get IL bytes
          byte[] ilBytes = myAction.GetMethodInfo().GetMethodBody().GetILAsByteArray();
          DynamicMethod dynamicCallback = new DynamicMethod("myAction", typeof(void), new Type[] { typeof(string) });
          DynamicILInfo dynamicIlInfo = dynamicCallback.GetDynamicILInfo();
          dynamicIlInfo.SetCode(ilBytes, 100);
          dynamicCallback.Invoke(null, new object[] { "World" });
      }
  }
  </code></pre>
  
  <p>When calling a <code>dynamicCallback.Invoke(null, new object[] { "World" })</code> we get "Exception thrown: 'System.BadImageFormatException' in mscorlib.dll".</p>
  
  <p>One thing I have no idea abut is what I should use as second argument for <code>SetCode()</code>, what should be used as 'maxStackSize'? How can I set the same value as for the initial action? But I suppose this is not the reason for the exception.</p>
  
  <p>How can I properly create a dynamic method from the IL bytes?</p>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <p>Here I would like to summarize the complete solution provided by Dudi Keleti:</p>
  
  <pre><code>static void Main(string[] args)
  {
      Action&lt;string&gt; myAction = s =&gt;
      {
          Console.WriteLine("Hello " + s);
      };
      MethodInfo method = myAction.GetMethodInfo();
      object target = myAction.Target;
  
      DynamicMethod dm = new DynamicMethod(
          method.Name,
          method.ReturnType,
          new[] {method.DeclaringType}.
              Concat(method.GetParameters().
                  Select(pi =&gt; pi.ParameterType)).ToArray(),
          method.DeclaringType,
          skipVisibility: true);
  
      DynamicILInfo ilInfo = dm.GetDynamicILInfo();
      var body = method.GetMethodBody();
      SignatureHelper sig = SignatureHelper.GetLocalVarSigHelper();
      foreach (LocalVariableInfo lvi in body.LocalVariables)
      {
         sig.AddArgument(lvi.LocalType, lvi.IsPinned);
      }
      ilInfo.SetLocalSignature(sig.GetSignature());
      byte[] code = body.GetILAsByteArray();
      ILReader reader = new ILReader(method);
      DynamicMethodHelper.ILInfoGetTokenVisitor visitor = new DynamicMethodHelper.ILInfoGetTokenVisitor(ilInfo, code);
      reader.Accept(visitor);
      ilInfo.SetCode(code, body.MaxStackSize);
  
      dm.Invoke(target, new object[] { target, "World" });
  
      Console.ReadLine(); //Just to see the result
  }
  </code></pre>
  
  <p>Note: DynamicMethodHelper is class developed by Haibo Luo and described in a <a href="https://blogs.msdn.microsoft.com/haibo_luo/2006/11/07/turn-methodinfo-to-dynamicmethod/" rel="nofollow">blog post</a> but can also be downloaded directly <a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/01/02/35/08/DynamicMethodHelper.cs" rel="nofollow">here</a>.</p>