目标: 从.NET 4.7控制台应用程序,使用Assembly.GetType()的反射,我尝试从程序集X中提取netstandard 2.0类的类型.然后我想用Activator.CreateInstance()创建此类型的实例.
我想要做的是: 但是,这个程序集X依赖于netstandard 2.0.为了能够获取Type,必须将netstandard依赖项加载到AppDomain中.这就是为什么当AppDomain通过AssemblyResolve事件请求netstandard程序集时,我只需加载这样的dll:
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);
Run Code Online (Sandbox Code Playgroud)
哪个投掷:
System.BadImageFormatException:'无法加载文件或程序集'文件:/// C:\ Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build \netstandard2.0\ref \netstandard.dll'或其依赖项之一.不应加载引用程序集以执行.它们只能在Reflection-only loader上下文中加载.(HRESULT异常:0x80131058)'
内部异常:BadImageFormatException:无法加载引用程序集以执行.
我所知道的: 我知道他们希望我们使用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库,但是它没有改变任何东西.
问题: 有没有人知道是否有正确的方法来加载该DLL而没有错误,或者可能是否是一个错误,或者?或者为什么这种DLL无法加载执行?