title: Generate exe in .Net
body: <p>In .Net, you can generate byte code in memory, and presumably save the resulting program to a .exe file. To do the first step, I have the following test code adapted from <a href="http://www.code-magazine.com/Article.aspx?quickid=0301051" rel="nofollow noreferrer">http://www.code-magazine.com/Article.aspx?quickid=0301051</a></p>
  
  <pre><code>var name = new AssemblyName();
  name.Name = "MyAssembly";
  var ad = Thread.GetDomain();
  var ab = ad.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
  var mb = ab.DefineDynamicModule("MyModule");
  var theClass = mb.DefineType("MathOps", TypeAttributes.Public);
  var retType = typeof(System.Int32);
  var parms = new Type[2];
  parms[0] = typeof(System.Int32);
  parms[1] = typeof(System.Int32);
  var meb = theClass.DefineMethod("ReturnSum", MethodAttributes.Public, retType, parms);
  var gen = meb.GetILGenerator();
  gen.Emit(OpCodes.Ldarg_1);
  gen.Emit(OpCodes.Ldarg_2);
  gen.Emit(OpCodes.Add_Ovf);
  gen.Emit(OpCodes.Stloc_0);
  gen.Emit(OpCodes.Br_S);
  gen.Emit(OpCodes.Ldloc_0);
  gen.Emit(OpCodes.Ret);
  theClass.CreateType();
  </code></pre>
  
  <p>How do you do the second step, and save the result to a .exe?</p>