Reputation: 39220
I've read docs for GetEntryAssembly and GetExecutingAssembly trying to make sense of the difference between them. I simply fail to understand how the definitions relate to each other. Altough I see two different formulations, I can't understand the distinction implied. In my head, it's a potayto-potahto situation, which is reinforced by the same contents on my screen when I try to display the values of each returned Assemby object.
Naturally, there must be some difference and it's simply my competence that prevents me from realizing what it is. So I've done some research, only discovering that most of the wisdom out there is about obtaining the path. The lonely resource that was explicitly targeting the comparison between them was here.
Can I ask for a specific example where those two methods return objects the contents of which differ? Preferably with a brief explanation of why.
Upvotes: 18
Views: 7820
Reputation: 1
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 theGetEntryAssembly
method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.
References:
Assembly.GetEntryAssembly
Method - https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getentryassembly
Assembly.GetExecutingAssembly
Method - https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getexecutingassembly
Upvotes: 6
Reputation: 3019
Let's say you have a console project MyConsoleProject
that references library project MyLibrary
.
Inside MyConsoleProject
both Entry and Executing assemblies will be the same. But inside MyLibrary
the ExecutingAssembly will refer to library project, not the console one.
Upvotes: 26