'无法加载文件或程序集'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

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

问题描述

目标:
从.NET 4.7控制台应用程序中,使用Assembly.GetType()进行反射,我试图从Assembly X中提取netstandard 2.0类的Type。然后,要使用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。为了获得Type,必须将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:\Users\xxx\.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll";

return Assembly.LoadFrom(netStandardDllPath);

哪个抛出:


System.BadImageFormatException:'无法加载文件或程序集
'file:/// C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0- Preview1-25301-01\build\netstandard2.0\ref\netstandard.dll'
或其依赖项之一。参考程序集不应加载
来执行。它们只能在仅反射的加载器
上下文中加载。 (来自HRESULT的异常:0x80131058)

System.BadImageFormatException: 'Could not load file or assembly 'file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.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()实例化该类型。请参见 Microsoft官方帖子

此外,我尝试引用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 Studio\2017\Community\MSBuild\Microsoft\Microsoft。 NET.Build.Extensions\net461\lib\netstandard.dll 或从.NET Core 2.0 SDK中,您可以找到它 C:\Program Files\dotnet \sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.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 Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll or from the .NET Core 2.0 SDK you can find it C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

在您的方案中尝试使用这些版本之一。

Try using one of those versions in your scenario.

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

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