我曾经认为一个程序集只能有一个 main() 方法,直到我在 Jon Skeet 在哥本哈根微软办公室的视频讲座中看到他的 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 镜像的 list 或某些 COFF header 中,在反汇编程序中看不到但在十六进制编辑器中可以看到?
最佳答案
我刚刚在 IL 反汇编器中打开了我的一个可执行文件。请注意 Main 方法的 .entrypoint 行。
.method public hidebysig static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 )
// Code size 22 (0x16)
.maxstack 1
.locals init ([0] class AuctionSniper.Main.App app)
IL_0000: nop
... <snipped>
与非入口点方法相比——比方说 InitializeComponent()
.method public hidebysig instance void InitializeComponent() cil managed
{
.custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 )
// Code size 20 (0x14)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
... <snipped>
关于c# - 程序集中写的程序集的入口点信息在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3337359/