如何将 .entrypoint 指令添加到方法(动态汇编)
我想使用 System.Reflection.Emit 中的类创建一个简单的应用程序。 如何将 enrypoint 指令添加到 Main 方法中?
AssemblyName aName = new AssemblyName("Hello");
AssemblyBuilder aBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
ModuleBuilder mBuilder = aBuilder.DefineDynamicModule("Module");
TypeBuilder tb = mBuilder.DefineType("Program", TypeAttributes.Public);
MethodBuilder methodBuilder = tb.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("Hello!");
aBuilder.SetEntryPoint(methodBuilder);
tb.CreateType();
aBuilder.Save("Hello.exe");
AssemblyBuilder.SetEntryPoint 似乎没有实现这一点。
I want to create a simple application using the classes in System.Reflection.Emit. How can I add the enrypoint directive to the Main method?
AssemblyName aName = new AssemblyName("Hello");
AssemblyBuilder aBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
ModuleBuilder mBuilder = aBuilder.DefineDynamicModule("Module");
TypeBuilder tb = mBuilder.DefineType("Program", TypeAttributes.Public);
MethodBuilder methodBuilder = tb.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("Hello!");
aBuilder.SetEntryPoint(methodBuilder);
tb.CreateType();
aBuilder.Save("Hello.exe");
AssemblyBuilder.SetEntryPoint does not seem to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(我已经在修改后的行上添加了评论):
Try this (I've put comments on modified lines):
看看他的示例,我已经我自己尝试了代码,效果非常好。
Have a look att his example, I've just tried the code myself and it works very nicley.