title: MethodBuilder.CreateMethodBody() problem in Dynamic Type creation
body: <p>For an experiment, i am trying to read the method body (using <strong>GetILAsByteArray()</strong>) from source type and adding it to the new Type (Using <strong>CreateMethodBody()</strong>).</p>
  
  <p>My source class is simply this</p>
  
  <pre><code>public class FullClass
  {
      public string Test(string data)
      {
          return data;
      }
      public string Test2(string data)
      {
          return data;
      }
      public string Test5(string data, string data1)
      {
          return data + data1;
      }
  }
  </code></pre>
  
  <p>The IL generated for this code (taken using reflector)</p>
  
  <pre><code>.method public hidebysig instance string Test(string data) cil managed
  {
      .maxstack 1
      .locals init (
          [0] string CS$1$0000)
      L_0000: nop 
      L_0001: ldarg.1 
      L_0002: stloc.0 
      L_0003: br.s L_0005
      L_0005: ldloc.0 
      L_0006: ret 
  }
  </code></pre>
  
  <p>But the IL generated from my new type looks like this</p>
  
  <pre><code>.method public hidebysig virtual instance string Test(string) cil managed
  {
      .maxstack 0
      L_0000: nop 
      L_0001: ldarg.1 
      L_0002: stloc.0 
      L_0003: br.s L_0005
      L_0005: ldloc.0 
      L_0006: ret 
  }
  </code></pre>
  
  <p>Differences are maxstack value &amp; .locals directive. I dont understand why my the actual class generates the locals, though it doesnt have any local variables??</p>
  
  <p>And why the differences in .maxstack value,since am using the same IL from the source to create the new Type.?</p>
  
  <p>Due to this am getting an error <strong>"Common Language Runtime detected an invalid program"</strong> while calling the method.</p>
  
  <p>My code creating the Dynamic type looks like this</p>
  
  <pre><code>public static class Mixin&lt;Target&gt;
      {
  
         public static Target compose&lt;TSource&gt;()
          {
              Type newType = null;
  
              AppDomain currentDom = Thread.GetDomain();
  
              AssemblyName DAssembly = new AssemblyName();
              DAssembly.Name = "DynamicTypesAssembly";
  
              AssemblyBuilder DAssemblyBldr = currentDom.DefineDynamicAssembly(
                                 DAssembly,
                                 AssemblyBuilderAccess.RunAndSave);
  
  
  
              ModuleBuilder DModuleBldr = DAssemblyBldr.DefineDynamicModule(DAssembly.Name, DAssembly.Name + ".dll", false);
           //   var DInterface = EmitInterface(DModuleBldr);
              TypeBuilder TypeBldr = DModuleBldr.DefineType("WorkOut.DType",
                      TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Serializable
                      ,typeof(object), new[] { typeof(Target) });
  
              //TypeBldr.AddInterfaceImplementation(typeof(DInterface));
  
              var methodCol = typeof(Target).GetMethods(BindingFlags.Public| BindingFlags.Instance);
  
              foreach (var ms in methodCol)
              {
                  var paramCol = ms.GetParameters();
                  var paramTypeArray = paramCol.Select(x =&gt; x.ParameterType).ToArray();
                  var paramNameArray = paramCol.Select(x=&gt;x.Name).ToArray();
                  MethodBuilder MthdBldr = TypeBldr.DefineMethod(ms.Name,
                                    MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
                                    ms.ReturnType,
                                    paramTypeArray);
  
                  for(int i=0;i&lt;paramCol.Count();i++)
                  {
                      MthdBldr.DefineParameter(i+1, ParameterAttributes.None, paramNameArray[i]);
                  }
  
  
                  MethodInfo[] methodInfos = typeof(TSource).GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
                               BindingFlags.Static | BindingFlags.Instance);
  
                  for (int i = 0; i &lt; methodInfos.Count(); i++)
                  {
                      var paramSrc = methodInfos[i].GetParameters();  
                      var paramSrcTypeArray = paramSrc.Select(x =&gt; x.ParameterType).ToArray();
  
                      if (methodInfos[i].Name == ms.Name &amp;&amp; methodInfos[i].ReturnType == ms.ReturnType &amp;&amp; paramSrc.Count() == paramCol.Count() &amp;&amp; paramTypeArray.SequenceEqual(paramSrcTypeArray))
                       {
                          var ILcodes = methodInfos[i].GetMethodBody().GetILAsByteArray();
                          var ilGen = MthdBldr.GetILGenerator();
                          //ilGen.Emit(OpCodes.Ldarg_0); //Load the 'this' reference onto the evaluation stack
                          //ilGen.Emit(OpCodes.Initobj);
                          MthdBldr.CreateMethodBody(ILcodes, ILcodes.Length);
                          //ilGen.Emit(OpCodes.Ret);
                          break;
                      }
                  }
  
              }
              newType = TypeBldr.CreateType();
              DAssemblyBldr.Save("a.dll");
              return (Target)Activator.CreateInstance(newType);  
          }
  </code></pre>
  
  <p>And code for invoking this is</p>
  
  <pre><code>     var resMix = Mixin&lt;ITest&gt;.compose&lt;FullClass&gt;();
       var returned1 = resMix.Test("sam");
  </code></pre>
  
  <p><strong>Edit:</strong> And the ITest (Target) interface is</p>
  
  <pre><code>public interface ITest
  {
       string Test(string data);     
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>when commenting this line</p>
  
  <pre><code>  //var ilGen = MthdBldr.GetILGenerator();
  </code></pre>
  
  <p>maxstack becomes <strong>.maxstack 16</strong></p>
  
  <p>I ran a check against the new dll against <strong>PEverify</strong> tool, this gives following error</p>
  
  <p><strong>WorkOut.DType::Test][offset 0x00000002] Unrecognized local variable number.</strong></p>
  
  <p>Any help really appreciated.... :)</p>