动态加载程序集的兼容性 [英] Compatibility of dynamically loaded assemblies
问题描述
我想在运行时动态加载 dotnet 程序集.
假设我有 netcore 3.1 控制台应用程序:
- 我可以加载 .net 标准程序集吗?如果是,我可以加载什么特定版本?
- 我可以加载经典的 .net 程序集,例如 .net framework 4.8 吗?如果是,我还可以加载旧版本吗?
相反的情况,当我有 dotnet framework 4.8 控制台应用程序时:
- 我可以加载 .net 标准程序集吗?
- 我可以加载旧的经典 .net,如程序集、.net 框架 4.6 吗?
.NET Standard 是一个接口 - 您可以调用的 API 版本列表.
.NET 标准可以被 .NET 框架和 .NET 核心引用.
<块引用>这是什么意思?
您应该对类库使用 .Net 标准.
.NET 框架和 .NET 核心这两个目标是一个平台..Net 框架仅适用于 Windows,而 .Net 核心适用于所有三个(Windows/Linux/MacOS)操作系统.这些不应相互引用.
<块引用>这就是 .Net 标准存在的原因.
以便快速回答您的问题.
- 我可以加载 .net 标准程序集吗?如果是,我可以加载什么特定版本?是的,您应该使用 .Net 标准
- 我可以加载经典的 .net 程序集,例如 .net framework 4.8 吗?如果是,我还可以加载旧版本吗?你可能可以,当然你不应该
相反的情况,当我有 dotnet framework 4.8 控制台应用程序时:
- 我可以加载 .net 标准程序集吗?是的!
- 我可以加载较旧的经典 .net,例如程序集、.net 框架 4.6 吗?是的!但是,在这里好好看看 -> <块引用>
为了避免定位错误和传递依赖错误或至少将它们保持在最低限度.使用 .NET 标准时,将包管理更改为 PackageReference.
PackageReference 以下链接提供了完整信息以及针对此类错误的一些故障排除技巧.
I want to dynamically load dotnet assemblies at runtime.
Let's say I have netcore 3.1 console app:
- Can I load .net standard assemblies? If yes what specific version I can load?
- Can I load classic .net assemblies, like .net framework 4.8? If yes can I also load older versions?
What about reverse situation, when I have dotnet framework 4.8 console app:
- Can I load .net standard assemblies?
- Can I load older classic .net, like assemblies,.net framework 4.6?
解决方案.NET Standard is an interface - a versioned list of APIs that you can call.
.Net standard can be referenced by both .NET framework and .NET core.
What does this mean?
You should use .Net standard for class libraries.
.NET framework and .NET core those two target a platform. .Net framework only works on Windows while .Net core works on all three(Windows/Linux/MacOS) operating systems. These should not reference one another.
This is why .Net standard exists.
So to quickly answer your questions.
- Can I load .net standard assemblies? If yes what specific version I can load? Yes, you should use .Net standard
- Can I load classic .net assemblies, like .net framework 4.8? If yes can I also load older versions? You probably can, for sure you should not
What about reverse situation, when I have dotnet framework 4.8 console app:
- Can I load .net standard assemblies? Yes!
- Can I load older classic .net, like assemblies,.net framework 4.6? Yes! But, take a good look here -> https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/version-compatibility#Apps
This is for .NET Standard. Taken from Microsoft Docs.
The documentation is very clear on what you need to pay attention to. The text below is similar to the one in the screenshot. I added here to be more readable.
The following table lists the minimum platform versions that support each .NET Standard version. That means that later versions of a listed platform also support the corresponding .NET Standard version. For example, .NET Core 2.2 supports .NET Standard 2.0 and earlier.
Guide
In addition to this ->https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support
In order to avoid targeting errors and transitive dependencies errors or at least keep them to a minimum. Change your package management to PackageReference when using .NET standard.
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
The link below provides the full info and also some troubleshooting tips for such errors.
Source: https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
这篇关于动态加载程序集的兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!