How to get current assembly in C#

By FoxLearn 11/28/2024 12:43:14 PM   31
To get the current assembly in C#, you can use the System.Reflection namespace.

For example, get the assembly of the current executing code.

// c# get current assembly
// Gets the assembly of the currently executing code
Assembly currentAssembly = Assembly.GetExecutingAssembly();
Console.WriteLine(currentAssembly.FullName); // Output: ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Use when you want the assembly where the code is running.

For example, get the assembly containing the type of the calling code.

// Gets the assembly of the code containing this type
Assembly currentAssembly = typeof(YourTypeName).Assembly;
Console.WriteLine(currentAssembly.FullName); // Output: ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Use when you want the assembly where a specific type is defined.

For example, get the entry assembly.

// Gets the entry assembly (e.g., the main application executable)
Assembly? entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
    Console.WriteLine(entryAssembly.FullName); // Output: ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Use when you want the assembly that contains the entry point of the application.