GetEntryAssembly 和 GetExecutingAssembly 之间的区别

Difference between GetEntryAssembly and GetExecutingAssembly

我阅读了 GetEntryAssembly and GetExecutingAssembly 的文档,试图弄清它们之间的区别。我根本不明白这些定义是如何相互关联的。虽然我看到了两种不同的表述,但我无法理解其中隐含的区别。在我看来,这是一种 potayto-potahto 情况,当我尝试显示每个 returned 组装对象。

当然,肯定有一些区别,只是我的能力使我无法意识到它是什么。所以我做了一些 research, only discovering that most of the wisdom out there is about obtaining the path. The lonely resource that was explictly targetting the comparison between them was a dead link.

我能问一个具体的例子吗,这两个方法 return 对象的内容不同?最好简要说明原因。

假设您有一个引用库项目 MyLibrary 的控制台项目 MyConsoleProject

MyConsoleProject 中,入口程序集和执行程序集都是相同的。但在 MyLibrary 中,ExecutingAssembly 将引用库项目,而不是控制台项目。

GetExecutingAssembly:

Gets the assembly that contains the code that is currently executing.

GetEntryAssembly returns:

The assembly that is the process executable in the default application domain, or the first executable that was executed by ExecuteAssembly(String). Can return null when called from unmanaged code.

The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.

参考文献: