'无法加载文件或程序集 'netstandard, Version=2.0.0.0, ...'.不应为执行加载引用程序集 [英] 'Could not load file or assembly 'netstandard, Version=2.0.0.0, ...'. Reference assemblies should not be loaded for execution

查看:182
本文介绍了'无法加载文件或程序集 'netstandard, Version=2.0.0.0, ...'.不应为执行加载引用程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:从 .NET 4.7 控制台应用程序中,使用反射与 Assembly.GetType(),我尝试从 Assembly X 中提取 netstandard 2.0 类的类型.然后我想使用 Activator.CreateInstance() 创建此类型的实例.

Goal: From a .NET 4.7 console app, using reflection with Assembly.GetType(), I am trying extract the Type of a netstandard 2.0 class from Assembly X. Then I want to create an instance of this Type with Activator.CreateInstance().

我想做什么:但是,这个程序集 X 依赖于 netstandard 2.0.为了能够获得类型,必须将 netstandard 依赖项加载到 AppDomain 中.这就是为什么当 AppDomain 通过 AssemblyResolve 事件请求 netstandard 程序集时,我只是像这样加载 dll:

What I am trying to do: However, this assembly X has a dependency to netstandard 2.0. To be able to get the Type, netstandard dependency has to be loaded into the AppDomain. That's why when the AppDomain is requesting the netstandard assembly through the AssemblyResolve event, I simply load the dll like this :

var netStandardDllPath = @"C:Usersxxx.nugetpackagesNETStandard.Library.2.0.0-preview1-25301-01uild
etstandard2.0
ef
etstandard.dll";

return Assembly.LoadFrom(netStandardDllPath);

抛出:

System.BadImageFormatException: '无法加载文件或程序集'file:///C:Usersvincent.lerouvilois.nugetpackagesNETStandard.Library.2.0.0-preview1-25301-01uild etstandard2.0 ef etstandard.dll'或其依赖项之一.不应加载引用程序集为执行.它们只能在 Reflection-only 加载器中加载语境.(来自 HRESULT 的异常:0x80131058)'

System.BadImageFormatException: 'Could not load file or assembly 'file:///C:Usersvincent.lerouvillois.nugetpackagesNETStandard.Library.2.0.0-preview1-25301-01uild etstandard2.0 ef etstandard.dll' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)'

内部异常:BadImageFormatException:无法加载引用组装执行.

Inner Exception: BadImageFormatException: Cannot load a reference assembly for execution.

我所知道的:我知道他们希望我们使用 Assembly.ReflectionOnlyLoadFrom 加载 DLL.但是这样做会阻止我使用 Activator.CreateInstance() 实例化类型.请参阅 微软官方帖子

What I know: I know that they want us to load the DLL with Assembly.ReflectionOnlyLoadFrom. But doing that will prevent me from instanciate the type with Activator.CreateInstance(). See Microsoft official post

此外,我尝试在我的控制台应用程序中引用 Nuget 包 NETStandard.Library 2.0.0-preview1-25301-01 和 NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 以便它具有 netstandard 2.0 库引用,但它没有改变任何东西.

Also, I tried referencing the Nuget packages NETStandard.Library 2.0.0-preview1-25301-01 and NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 in my console app so it would have the netstandard 2.0 libraries referenced, but it didn't change anything.

问题:有谁知道是否有正确的方法可以无错误地加载该 dll,或者这是否是一个错误,或者其他?或者为什么这种dll无法加载执行?

Question: Does anyone would know if there is a proper way to load that dll without error, or maybe if this is a bug, or else? Or why this kind of dll is not able to load for execution?

推荐答案

您尝试加载的 netstandard.dll 是一个参考程序集,正如其他人指出的那样,无法在 .NET Framework 上为运行时加载该程序集.但是,如果您需要解决该依赖关系,您将需要映射到您尝试运行的框架的运行时版本.

The netstandard.dll you are trying to load is a reference assembly that which cannot be loaded for runtime on .NET Framework as pointed out by others. However if you need to resolve that dependency you will need to runtime version that maps to the framework you are trying to run on.

对于 .NET Standard 支持,我们将它们作为 VS 附带的 msbuild 扩展的一部分包含在内,因此您需要从那里获取 netstandard.dll 版本.根据您安装的 VS2017 版本,它应该是类似于 C:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuildMicrosoftMicrosoft.NET.Build.Extensions et461lib etstandard.dll 或从 .NET Core 2.0 SDK 你可以找到它 C:Program Filesdotnetsdk2.0.0MicrosoftMicrosoft.NET.Build.Extensions et461lib etstandard.dll

For .NET Standard support we are including them as part of the msbuild extensions that ship with VS so you will want to get the version of netstandard.dll from there. Depending on which version of VS2017 you have installed it should be somewhere like C:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuildMicrosoftMicrosoft.NET.Build.Extensions et461lib etstandard.dll or from the .NET Core 2.0 SDK you can find it C:Program Filesdotnetsdk2.0.0MicrosoftMicrosoft.NET.Build.Extensions et461lib etstandard.dll

尝试在您的场景中使用这些版本之一.

Try using one of those versions in your scenario.

这篇关于'无法加载文件或程序集 'netstandard, Version=2.0.0.0, ...'.不应为执行加载引用程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆