c# - Reflection.Emit: AssemblyBuilder.SetEntryPoint does not set entry point -
i writing concatenative language in c#, , interpreted, want take next step: compiling. start on this, tried writing simple "hello, world!" program emitter using system.reflection.emit. code works without emit exceptions, when run generated "test.exe" file, throws exception
unhandled exception: system.missingmethodexception: entry point not found in assembly 'il_test, version=0.0.0.0, culture=neutral, publickeytoken=null'.
i have tried googling answer, no avail. perhaps here can me? (please) code wrote below:
using system; using system.collections.generic; using system.linq; using system.text; using system.reflection; using system.reflection.emit; using system.io; using system.diagnostics; namespace ilcompiletest { class program { static void main(string[] args) { const string assembly_name = "il_test"; assemblybuilder assemblybuilder = appdomain.currentdomain.definedynamicassembly( new assemblyname(assembly_name), assemblybuilderaccess.save); modulebuilder modulebuilder = assemblybuilder.definedynamicmodule( assembly_name); typebuilder typebuilder = modulebuilder.definetype("program", typeattributes.class | typeattributes.public); methodbuilder methodbuilder = typebuilder.definemethod( "main", methodattributes.public | methodattributes.static, typeof(void), new type[] { typeof(string[]) }); ilgenerator gen = methodbuilder.getilgenerator(); gen.emit(opcodes.ldstr, "hello, world!"); gen.emit(opcodes.call, typeof(console).getmethod("writeline", new type[] { typeof(string) })); gen.emit(opcodes.ldc_i4_1); gen.emit(opcodes.call, typeof(console).getmethod("readkey", new type[] { typeof(bool) })); assemblybuilder.setentrypoint(methodbuilder, pefilekinds.consoleapplication); file.delete("test.exe"); assemblybuilder.save("test.exe"); process.start("test.exe"); } } }
so, question is: how can set entry point main method define?
you missing call typebuilder.createtype(), definedynamicmodule must have exe name passed second parameter. full working sample:
using system; using system.collections.generic; using system.linq; using system.text; using system.reflection; using system.reflection.emit; using system.io; using system.diagnostics; namespace ilcompiletest { class program { static void main(string[] args) { const string assembly_name = "il_test"; assemblybuilder assemblybuilder = appdomain.currentdomain.definedynamicassembly( new assemblyname(assembly_name), assemblybuilderaccess.save); modulebuilder modulebuilder = assemblybuilder.definedynamicmodule( assembly_name, "test.exe"); typebuilder typebuilder = modulebuilder.definetype("program", typeattributes.class | typeattributes.public); methodbuilder methodbuilder = typebuilder.definemethod( "main", methodattributes.hidebysig|methodattributes.public | methodattributes.static, typeof(void), new type[] { typeof(string[]) }); ilgenerator gen = methodbuilder.getilgenerator(); gen.emit(opcodes.ldstr, "hello, world!"); gen.emit(opcodes.call, typeof(console).getmethod("writeline", new type[] { typeof(string) })); gen.emit(opcodes.ldc_i4_1); gen.emit(opcodes.call, typeof(console).getmethod("readkey", new type[] { typeof(bool) })); typebuilder.createtype(); assemblybuilder.setentrypoint(methodbuilder, pefilekinds.consoleapplication); file.delete("test.exe"); assemblybuilder.save("test.exe"); process.start("test.exe"); } } }
Comments
Post a Comment