简体   繁体   中英

Can NET Framework 4.7.2 apps load at runtime NET Core 5.0 library DLLs?

I'm starting to port some of my NET Framework 4.7.2 class libraries to NET Core 5.0. The host Framework app dynamically discovers and runs Assembly.Load on the NET Core library ok. But when I try to do a Assembly.GetType(NET50Namespace.Classname) operation on the loaded assembly, it fails and returns null with the error:

{"Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

It looks like it is trying to load the NET Core 5.0.0.0 runtime, which I do have installed. I checked with the console command dotnet --list-runtimes and both the NETCore and WindowsDesktop runtimes are installed ok:

c> dotnet --list-runtimes

Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

I'm stuck.

Q1. Is it legal/possible for a Framework 4.7.2 app to dynamically load and run a NETCore 5.0 DLL? I was trying to port small pieces of my app to NET50 as a first step. But it is not working, and I might be trying to do the impossible.

Q2. If it is ok for Framework to load/call NET50 DLLs, what am I doing wrong, and how can I make the GetType call work so that I can instantiate and call the NET50 class library?

Thank you.

"No"

Fundamentally, that is not a supported scenario, and there are hard breaks between .NET Framework and .NET Core (which essentially became .NET 5).

If it helps, .NET 4.7.2 "kinda mostly sort of" supports .NET Standard 2.0, which .NET 5 also implements (correctly), so if your library can target .NET Standard 2.0 it could potentially be loaded by both a .NET 5 and .NET Framework 4.7.2 application. You could also multi-target the library with multiple TFMs, for example net472 and net5.0 (perhaps with some netstandard too, for good measure)

However, quite honestly: it is only going to get harder and harder to keep working with .NET Framework; if at all possible, you should prioritise moving to a more recent .NET version instead (such as .NET Core 3.1 or .NET 5, at time of writing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM