gpt4 book ai didi

c# - 如何在 .Net Framework 4.8 中使用 .Net Standard 2.1 DLL?

转载 作者:行者123 更新时间:2023-12-04 11:20:40 34 4
gpt4 key购买 nike

我有一个针对 .Net Framework 4.8 的项目。现在,我们需要使用第 3 方 dll。问题是 dll 是针对 .Net Standard 2.1 的。
有没有办法在 .Net Framework 4.8 项目中使用这个 dll?
我添加了dll,得到这个错误:

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
我搜索了,似乎它应该直接工作。也许我需要添加一个引用?不确定现在需要做什么。
谢谢

最佳答案

您不能在任何 .Net Framework 版本中使用 .Net Standard 2.1 程序集,因为 .NET Framework(甚至是最后一个版本,4.8)没有实现 .Net Standard 2.1。
.Net Standard .NET 实现支持矩阵位于 https://docs.microsoft.com/en-us/dotnet/standard/net-standard在“框架”下不包含 2.1 的条目。
有关 MS 决定的更多详细信息:https://devblogs.microsoft.com/dotnet/announcing-net-standard-2-1/
提炼:

Given many of the API additions in .NET Standard 2.1 require runtimechanges in order to be meaningful, .NET Framework 4.8 will remain on.NET Standard 2.0 rather than implement .NET Standard 2.1. .NET Core3.0 as well as upcoming versions of Xamarin, Mono, and Unity will be updated to implement .NET Standard 2.1.

Library authors who need to support .NET Framework customers shouldstay on .NET Standard 2.0. In fact, most libraries should be able tostay on .NET Standard 2.0, as the API additions are largely foradvanced scenarios. However, this doesn’t mean that library authorscannot take advantage of these APIs even if they have to support .NETFramework. In those cases they can use multi-targeting to compile forboth .NET Standard 2.0 as well as .NET Standard 2.1. This allowswriting code that can expose more features or provide a more efficientimplementation on runtimes that support .NET Standard 2.1 while notgiving up on the bigger reach that .NET Standard 2.0 offers.


解决方案:
最简单的方法是说服第 3 方向后移植/多目标也支持 netstandard2.0。或者,您可以在项目中切换到面向 .Net Core 3.1 (LTS)/Mono 而不是 Framework 4.8。 (支持矩阵可能会随时间变化,这是撰写本文时支持的列表)。
更新:
另一种可能的解决方法:
您还可以尝试使用 .NET Framework 兼容模式进行一些技巧。我不知道你能做到这一点。例如,您可以制作一个以标准为目标的“包装器”项目,并同时引用您的 .Net Framework 4.8 代码和第 3 方库,除非 4.8 部分使用 WPF - 也许在不支持的场景中还有其他陷阱,如下所述。这对我来说听起来很复杂,但如果你别无选择,它可能值得一试。来自同一个原始站点:

Starting with .NET Standard 2.0, the .NET Framework compatibility modewas introduced. This compatibility mode allows .NET Standard (*) projectsto reference .NET Framework libraries as if they were compiled for.NET Standard. Referencing .NET Framework libraries doesn't work forall projects, such as libraries that use Windows PresentationFoundation (WPF) APIs.


(*) - 还有 .Net Core(在文档/页面的其他地方)

关于c# - 如何在 .Net Framework 4.8 中使用 .Net Standard 2.1 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63588099/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com