欢迎光临
我们一直在努力

Create Exe On Fly-.NET教程,Windows开发

建站超值云服务器,限时71元/月
creating exe on the fly
submitted by user level date of submission
kunal cheda beginner 06/29/2001

this is probably the most wonderful this i have explored in .net. what happens here is a user can actually make the exe file on the fly. system.runtime.emit namespace provides necessay class to do this.
after compiling this file and running it on the console a new file is generated in your folder called testasm.exe. this exe file prints a message "hello world" on the console.

source code:

runtimeemit.cs  
using system;
using system.runtime;
using system.runtime.emit;
class runtimeemit
{
public static void main(string [] args)
{
         appdomain ad = appdomain.currentdomain;
         assemblyname am = new assemblyname();
         am.name = "testasm";
        assemblybuilder ab = ad.definedynamicassembly(am,assemblybuilderaccess.save);
        modulebuilder mb = ab.definedynamicmodule("testmod","testasm.exe");
        typebuilder tb = mb.definetype("mytype",typeattributes.public);  
        methodbuilder metb = tb.definemethod("hi",methodattributes.public | methodattributes.static,null,null);
        mb.setentrypoint(metb);
        ilgenerator il = metb.getilgenerator();
        il.emitwriteline("hello world");
        il.emit(opcodes.ret);  
        tb.createtype();
        ab.save("testasm.exe");
}
}

save this file as runtimeemit.cs  and compile c:/>csc runtimeemit.cs and run this file c:\>runtimeemit  
to run the exe file generated  c:\>testasm

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Create Exe On Fly-.NET教程,Windows开发
分享到: 更多 (0)