GDB
GDB

Reputation: 3579

Assembly.GetExecutingAssembly() available in .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’

Upvotes: 31

Views: 26332

Answers (3)

MindingData
MindingData

Reputation: 12508

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.

Upvotes: 27

Polynomial
Polynomial

Reputation: 3706

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.

Upvotes: 36

Tseng
Tseng

Reputation: 64259

Nope, it's not available in .NET Core 1.0/1.1.

However, if I remember correctly it will be back in .NET Core 2.0 and the netstandard2.0 which is to be released later this year, which will have a much bigger API surface and increased compatibility with libraries written against .NET >= 4.5 making it easier to port these to .NETStandard 2.0/2.1.

But many of the APIs implementations will be platform dependent. Means, you will be able to call SomeAPI.GetSomething() everywhere, but if run on .NET Core it may throw a PlatformNotSupportedException.

But most APIs about assembly scanning and discovering should be back in .NET Core/Standard 2.0. Stuff around AppDomain will still be missing as there are no AppDomains in .NET Core and processes should be used for isolation.

Upvotes: 4

Related Questions