"Failed to load file or assembly netstandard, Version = 2.0.0.0, ...". Reference assemblies must not load to execute

Purpose: from a .NET 4.7 console application, using reflection with Assembly.GetType (), I try to extract a class class of netstandard 2.0 class from Assembly X. Then I want to instantiate this type using Activator.CreateInstance (),

What I'm trying to do: It does not , however, the assembly has a dependency on X Netstandard 2.0. To get a type, you need to load a custom dependency in the application domain. Therefore, when AppDomain requests a custom assembly through the AssemblyResolve event, I simply load the dll as follows:

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);

Which throws:

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 must not be loaded to execute. They can only be loaded in the boot loader context for reflection only. (Exception from HRESULT: 0x80131058) '

Internal exception: BadImageFormatException: Unable to load the reference assembly for execution.

What I know: I know that they want us to load the DLL using Assembly.ReflectionOnlyLoadFrom. But that will not allow me to instantiate the type using 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, .

: - , DLL , , , , ? DLL ?

+16
7

Netstandard.dll, , - , .NET Framework, . , , , , .

.NET Standard msbuild, VS, netstandard.dll . VS2017, , - C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll SDK.NET Core 2.0, C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

.

+11

Copy Enbale true netstandard.dll.

  1. netstandard.dll.
  2. Copy Enbale true.

enter image description here

+5

.

.NET Standard - API, .NET.

. , . , , .NET Standard 2.0.

: https://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs

:.NET Framework 4.7 .NET Standard 2.0, - Activator.CreateInstance() .NET.

+3

NETStandard 2.0.0-preview1 net461 net47.

.NET Core SDK 2.0 ( 2.0.0-preview2)

 var netStandardDllPath = @"c:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\netstandard.dll";
 Console.WriteLine(Assembly.LoadFrom(netStandardDllPath).FullName);

.

preview1, , netstandard2.0 net471.

+2

NetStandard.Library 2.0.0.0 NuGet, . .net Framework 4.6.1 4.6.0

0

: 1 - .Net Framework . 2 - Windows . 3 - , Nuget .

, 3

0

. , " ... ".

, .NET Framework ( .NET Framework, .NET Standard) .NET Framework 4.7.2, , , 4.7.2. .

, , , .NET Framework 4.7.2.

, , .NET Framework.

0

All Articles