Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.GetExecutingAssembly() available in .NET Core?

Tags:

asp.net-core

Need to embed a json file as a source for testing in my .NET Core application. The author of this post http://codeopinion.com/asp-net-core-embedded-resource/ provided sample code that included the use of var assembly = Assembly.GetExecutingAssembly();However when I try this I get the error: Cannot resolve symbol ‘GetExecutingAssembly’ and ‘Assembly’ does not contain a definition for ‘GetExecuringAssembly’

like image 536
GDB Avatar asked Jan 30 '17 19:01

GDB


People also ask

What is assembly GetExecutingAssembly ()?

Assembly property to get the currently executing assembly based on a type contained in that assembly. It also calls the GetExecutingAssembly method to show that it returns an Assembly object that represents the same assembly.

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 System reflection assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.


2 Answers

If you are targeting .NET Standard 1.5 or above, you may call any of the following:

System.Reflection.Assembly.GetExecutingAssembly(); System.Reflection.Assembly.GetEntryAssembly(); System.Reflection.Assembly.GetCallingAssembly(); 

If targeting earlier versions of .NET Standard then the typeof(SomeClass).GetTypeInfo().Assembly method is the only way.

like image 139
Polynomial Avatar answered Sep 21 '22 14:09

Polynomial


There is no "static" Assembly class in .NET Standard prior to version 1.5. Instead you have to do something like:

typeof(<AClassHere>).GetTypeInfo().Assembly 

Where <AClassHere> should be replaced by the name of a class/type in the assembly you wish to load.

like image 44
MindingData Avatar answered Sep 20 '22 14:09

MindingData