如何将 .entrypoint 指令添加到方法(动态汇编)

发布于 2024-07-28 22:20:39 字数 743 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-08-04 22:20:39

试试这个(我已经在修改后的行上添加了评论):

AssemblyName aName = new AssemblyName("Hello");
AssemblyBuilder aBuilder = AppDomain
    .CurrentDomain
    .DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
// When you define a dynamic module and want to save the assembly 
// to the disc you need to specify a filename
ModuleBuilder mBuilder = aBuilder
    .DefineDynamicModule("Module", "Hello.exe", false);
TypeBuilder tb = mBuilder
    .DefineType("Program", TypeAttributes.Public);
MethodBuilder methodBuilder = tb
    .DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);

ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("Hello!");

// You need to always emit the return operation from a method 
// otherwise you will get an invalid IL
ilGenerator.Emit(OpCodes.Ret);

aBuilder.SetEntryPoint(methodBuilder);
tb.CreateType();
aBuilder.Save("Hello.exe");

Try this (I've put comments on modified lines):

AssemblyName aName = new AssemblyName("Hello");
AssemblyBuilder aBuilder = AppDomain
    .CurrentDomain
    .DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);
// When you define a dynamic module and want to save the assembly 
// to the disc you need to specify a filename
ModuleBuilder mBuilder = aBuilder
    .DefineDynamicModule("Module", "Hello.exe", false);
TypeBuilder tb = mBuilder
    .DefineType("Program", TypeAttributes.Public);
MethodBuilder methodBuilder = tb
    .DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static);

ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("Hello!");

// You need to always emit the return operation from a method 
// otherwise you will get an invalid IL
ilGenerator.Emit(OpCodes.Ret);

aBuilder.SetEntryPoint(methodBuilder);
tb.CreateType();
aBuilder.Save("Hello.exe");
深海夜未眠 2024-08-04 22:20:39

看看他的示例,我已经我自己尝试了代码,效果非常好。

Have a look att his example, I've just tried the code myself and it works very nicley.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文