c# - 程序集中写的程序集的入口点信息在哪里?

标签 c# reflection assemblies entry-point

我曾经认为一个程序集只能有一个 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/

相关文章:

c# - 在业务应用程序中使用反射可能存在的弱点

C#停止程序的流程并在不重新启动的情况下将其重置

c# - 混合模式程序集是针对版本 'v1.1.4322' 构建的

java - 数据库到GlazedList/Jtable,然后通过GlazedList/JTable编辑数据库

scala - 修改 Scala AST 中的节点

.net - 程序集代码库 : when is it no file-URI?

.net - 为什么我无法将 DLL 添加到 Windows 7 中的 GAC?

c# - 加载由 Roslyn 编译器生成的程序集

c# - 在 linq 中设置相等性

C# DateTime,这种方法区域设置安全吗?