NET Framework 4.7.2应用程序可以在运行时加载NET Core 5.0库DLL吗? [英] Can NET Framework 4.7.2 apps load at runtime NET Core 5.0 library DLLs?

查看:203
本文介绍了NET Framework 4.7.2应用程序可以在运行时加载NET Core 5.0库DLL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始将某些NET Framework 4.7.2类库移植到NET Core 5.0.宿主Framework应用程序在NET Core库上动态发现并运行 Assembly.Load .但是,当我尝试对已加载的程序集执行 Assembly.GetType(NET50Namespace.Classname)操作时,它失败并返回null并出现错误:

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:

{无法加载文件或程序集'System.Runtime,版本= 5.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a"或其依赖项之一.系统找不到指定的文件.":"System.Runtime,版本= 5.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a"}

{"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"}

似乎正在尝试加载我已经安装的NET Core 5.0.0.0运行时.我用控制台命令 dotnet --list-runtimes 进行了检查,并且NETCore和WindowsDesktop运行时都已正确安装:

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]

我被困住了.

Q1.Framework 4.7.2应用程序动态加载并运行NETCore 5.0 DLL是否合法/可行?第一步,我试图将我的应用程序的小部分移植到NET50.但这是行不通的,我可能正在尝试做不可能的事情.

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.如果Framework可以加载/调用NET50 DLL,那我在做什么错了,如何使 GetType 调用起作用,以便可以实例化并调用NET50类库?

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?

谢谢.

推荐答案

否"

从根本上讲,这不是受支持的方案,.NET Framework和.NET Core(本质上是.NET 5)之间存在硬性中断.

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

如果有帮助,.NET 4.7.2可以说有点像"支持.NET 5也正确实现的.NET Standard 2.0,因此,如果您的库可以定位.NET Standard 2.0,则它可能同时由.NET 5和.NET Framework 4.7.2应用程序加载.您还可以使用多个TFM(例如, net472 net5.0 )(也可能带有一些 netstandard ,也可以作为多个目标)对库进行多目标定位)

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)

但是,说实话:继续使用.NET Framework只会越来越困难;如果可能的话,您应该优先考虑改用较新的.NET版本(例如在撰写本文时.NETCore 3.1或.NET 5).

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

这篇关于NET Framework 4.7.2应用程序可以在运行时加载NET Core 5.0库DLL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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