程序集的入口点信息写在哪里?
我曾经认为一个程序集只能有一个 main() 方法,直到我在 Jon Skeet 在哥本哈根 Microsoft 办公室发表的视频讲座中看到他的 MiscUtil。
因此,我编写了这个小应用程序,它有两个 main() 方法,如下所示:
namespace ManyMains
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
}
class YetAnotherProgram
{
static void Main()
{
Console.WriteLine("Yet another program.");
Console.ReadKey();
}
}
}
我在 Visual Studio 中设置了 StartUp 对象,它起作用了。好吧,没有理由难过。然后,我想查看这些信息到底存储在程序集中的位置,因此我在反射器中打开编译后的二进制文件,发现绝对没有任何元数据。
我想知道此类信息是否被写入 PE 映像的清单或某些 COFF 标头中,这些信息在反汇编程序中看不到,但可以在十六进制编辑器中看到?
I used to think that an assembly could have only one main() method until I saw Jon Skeet's MiscUtil in a video lecture he delivered at the Microsoft office in Copenhagen.
So, I wrote this little app that had two main() methods like so:
namespace ManyMains
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
}
class YetAnotherProgram
{
static void Main()
{
Console.WriteLine("Yet another program.");
Console.ReadKey();
}
}
}
I set the StartUp object in Visual Studio and it worked. Okay, no cause for distress. Then, I wanted to see where exactly this information is stored in the assembly, so I opened the compiled binary in reflector and saw absolutely no metadata to that effect.
I'm wondering if that sort of information is written into the manifest or some COFF header of the PE image that can't be seen in a disassembler but could be seen in a hex editor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚在 IL 反汇编器中打开了一个可执行文件。请注意 Main 方法的 .entrypoint 行。
与非入口点方法 - 让我们说 InitializeComponent()
I just opened up one of my executables in the IL Disassembler. Notice the .entrypoint line for the Main method.
vs a non-entry point method - Lets say InitializeComponent()
您可以使用 ildasm.exe 进行检查
You can check that using
ildasm.exe
在 PE 文件的 CLI 标头中偏移量 20 处,有入口点令牌。请参阅 ECMA 335 规范的第 25.3.3 节。
在 IL 中,您可以将
.entrypoint
指令放入方法主体中。该方法必须是静态的,没有参数或接受一组字符串。 (包括可变参数)。如果您将语言更改为 IL,您应该会在 Reflector 中看到这一点。In the CLI header of the PE file at offset 20, there is the entrypoint token. see section 25.3.3 of the ecma 335 specification.
In IL you would place the
.entrypoint
directive into a method body. The method must be static, have no parameters or accept a an aray of strings. (varargs included). you should see this in reflector if you change the language to IL.