Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Executing Assembly Name in .net core

I have a legacy class library targeting netstandard1.2 where we need to get the assembly name where this library is referenced.

Using the following give us the name of class library and it works fine.

typeof(MyHelperClass).GetTypeInfo().Assembly

but we need to get the referenced assembly name. Following code will work fine for standard .net but not for .net core

Assembly.GetEntryAssembly().GetName().Name;

Is there a way to get the executing assembly name using .net core while targeting .net standard 1.2?

like image 736
Haseeb Asif Avatar asked Jan 30 '18 17:01

Haseeb Asif


People also ask

How do I find my assembly name?

If you know the assembly's file system path, you can call the static (C#) or Shared (Visual Basic) AssemblyName. GetAssemblyName method to get the fully qualified assembly name.

How do you access the assembly of a running application?

The recommended way to retrieve an Assembly object that represents the current assembly is to use the Type. Assembly property of a type found in the assembly, as the following example illustrates. To get the assembly that contains the method that called the currently executing code, use GetCallingAssembly.

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

What is assembly GetExecutingAssembly ()?

In C#, GetExecutingAssembly() method is the method of Assembly class. This method returns the assembly that contains the code that is currently executing. To use this method we have to use System. Reflection in our program.


1 Answers

If it is above .NetCore 1.5 you can either use the following:

System.Reflection.Assembly.GetExecutingAssembly();

or (depending on your usecase):

System.Reflection.Assembly.GetCallingAssembly();

Don't forget to import the library:

using System.Reflection;
like image 157
Antonin GAVREL Avatar answered Sep 19 '22 17:09

Antonin GAVREL