31

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’

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
GDB
  • 3,379
  • 2
  • 26
  • 38
  • Possible duplicate of [How do you reference the executing assembly in DNX Core 5.0 (ASP.NET 5)?](http://stackoverflow.com/questions/32856659/how-do-you-reference-the-executing-assembly-in-dnx-core-5-0-asp-net-5) – Victor Hurdugaci Jan 30 '17 at 19:54

3 Answers3

36

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.

Polynomial
  • 3,656
  • 23
  • 36
  • 7
    @jgauffin `System.Reflection.Assembly.GetEntryAssembly()` is part of .NET Core. There are tests for it in `corefx`: https://github.com/dotnet/corefx/blob/6317e53b89680d83747d789c354bb1acda011758/src/System.Reflection/tests/AssemblyTests.cs#L120-L128 – Polynomial Apr 18 '17 at 07:06
  • @jgauffin dotnetcore is part of .NET standard. – K-Dawg Aug 20 '17 at 15:52
27

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.

Sergey
  • 1,608
  • 1
  • 27
  • 40
MindingData
  • 11,924
  • 6
  • 49
  • 68
4

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.

Tseng
  • 61,549
  • 15
  • 193
  • 205
  • In addition to this, check out [this link for the alternative.](https://forums.asp.net/t/2001385.aspx?Assembly+GetExecutingAssembly+core) – Rohit416 Nov 24 '17 at 09:17