C# 在程序集中写入的有关程序集入口点的信息在哪里?
我曾经认为程序集只能有一个main()方法,直到我看到Jon Skeet在哥本哈根的Microsoft office上发表的视频演讲中的MiscUtil 因此,我编写了这个小应用程序,它有两个main()方法,如下所示:C# 在程序集中写入的有关程序集入口点的信息在哪里?,c#,reflection,assemblies,entry-point,C#,Reflection,Assemblies,Entry Point,我曾经认为程序集只能有一个main()方法,直到我看到Jon Skeet在哥本哈根的Microsoft office上发表的视频演讲中的MiscUtil 因此,我编写了这个小应用程序,它有两个main()方法,如下所示: namespace ManyMains { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World
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();
}
}
}
我在visualstudio中设置了StartUp对象,它成功了。好吧,没什么好担心的。然后,我想看看这些信息到底存储在程序集中的什么地方,所以我在reflector中打开了编译后的二进制文件,但没有看到任何元数据
我想知道这类信息是否写入了PE映像的清单或某些COFF头中,这些信息在反汇编程序中看不到,但在十六进制编辑器中可以看到?您可以使用
ildasm.exe
ildasm /ALL /TEXT program.exe
我刚刚在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>
.method public hidebysing static void Main()cil managed
{
.入口点
.custom实例void[mscorlib]System.STAThreadAttribute::.ctor()=(01 00)
.custom instance void[mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor()=(01 00)
//代码大小22(0x16)
.maxstack 1
.locals init([0]类AuctionSniper.Main.App)
IL_0000:没有
...
vs一个非入口点方法-比方说InitializeComponent()
.method public hidebysing实例void InitializeComponent()cil managed
{
.custom instance void[mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor()=(01 00)
//代码大小20(0x14)
.maxstack 8
IL_0000:没有
IL_0001:ldarg.0
...
在偏移量为20的PE文件的CLI头中,有入口点标记。请参阅ecma 335规范第25.3.3节
在IL中,您可以将
.entrypoint
指令放入方法体。该方法必须是静态的,没有参数或接受一系列字符串(包括varargs)。如果您将语言更改为IL,您应该在reflector中看到这一点。哇!非常有趣。谢谢。:-)
.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>