8

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • `GetEntryAssembly` will always return the name of the assembly that was first executed (usually the executable name), `GetExecutingAssembly` returns the name of the assembly that contains the code that's actually being executed. – Gusman Jun 19 '20 at 06:18
  • See https://stackoverflow.com/questions/15407340/difference-between-assembly-getexecutingassembly-and-typeofprogram-assembly/18216908 and https://stackoverflow.com/questions/27059748/which-is-better-for-getting-assembly-location-getassembly-location-or-getexe#:~:text=GetExecutingAssembly%20returns%20the%20assembly%20where,may%20not%20be%20your%20executable. – Ian Ringrose Jun 19 '20 at 08:12

2 Answers2

17

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.

Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18
  • Ah, so for web projects (usually consisting of a single project with a WebApi) there's no difference, usually, then, right? Which would be the most intuitively first choice of those two in the cases where I know that the project won't be interacting with external libraries? In practice, they will produce the same result but I sense that one of htem is the primary choice unless the other is required. What do you think? – Konrad Viltersten Jun 19 '20 at 07:14
  • I think, once you know the difference between them, you will use them accordingly. – Tomas Chabada Jun 19 '20 at 07:28
  • Perhaps I wasn't clear on my follow-up. I do understand the difference between them, now, thanks to your answer. However, I'd like to hear your subjective opinion on which of those that you'd find most expected in the case when both can be used interchangeably due to the known scope of the application etc. – Konrad Viltersten Jun 19 '20 at 08:37
  • @KonradViltersten most of the time, I found `GetEntryAssembly` to be the first choice, then in specific cases go for `GetExecutingAssembly` – Tomas Chabada Jun 19 '20 at 08:51
3

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.

References:

Pang
  • 9,564
  • 146
  • 81
  • 122
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • So, as to my comment on the other answer, which of those would you intuitively expect to see in an application where the executing assembly will always coincide with the entry assembly? In a smaller application, for instance. I do understand what they do and how they differ, now. But I'd like to hear others' opinion on which is the default'ish one, or at least perceived as such, as it might be a subjective matter of taste and... well... opinion. – Konrad Viltersten Jun 19 '20 at 08:36
  • The OP already said that he read the docs, so only quoting the exact text here will probably not help much. Just my two cents though. – Christian.K Jun 19 '20 at 13:04