在.NET Core中可用的Assembly.GetExecutingAssembly()?

GDB*_*GDB 25 asp.net-core

需要在我的.NET Core应用程序中嵌入一个json文件作为测试源.这篇文章的作者http://codeopinion.com/asp-net-core-embedded-resource/ 提供了包含使用的示例代码var assembly = Assembly.GetExecutingAssembly();但是当我尝试这个时我收到错误:Cannot resolve symbol ‘GetExecutingAssembly’ and ‘Assembly’ does not contain a definition for ‘GetExecuringAssembly’

Pol*_*ial 30

如果您的目标是.NET Standard 1.5或更高版本,则可以调用以下任何一项:

System.Reflection.Assembly.GetExecutingAssembly();
System.Reflection.Assembly.GetEntryAssembly();
System.Reflection.Assembly.GetCallingAssembly();
Run Code Online (Sandbox Code Playgroud)

如果定位早期版本的.NET Standard,则该typeof(SomeClass).GetTypeInfo().Assembly方法是唯一的方法.

  • @jgauffin`System.Reflection.Assembly.GetEntryAssembly()`是.NET Core的一部分.在`corefx`中有测试:https://github.com/dotnet/corefx/blob/6317e53b89680d83747d789c354bb1acda011758/src/System.Reflection/tests/AssemblyTests.cs#L120-L128 (6认同)

Min*_*ata 24

在1.5版之前的.NET Standard中没有"静态"程序集类.相反,你必须做类似的事情

typeof(<AClassHere>).GetTypeInfo().Assembly
Run Code Online (Sandbox Code Playgroud)

在哪里应该替换为要加载的程序集中的类/类型的名称.