| 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
