开发者

Generate exe in .Net

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 http://www.code-magazine.com/Article.aspx?quickid=0301051

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(OpCode开发者_高级运维s.Ldloc_0);
gen.Emit(OpCodes.Ret);
theClass.CreateType();

How do you do the second step, and save the result to a .exe?


I think you might be looking for AssemblyBuilder.Save

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜