我可以在经典的 .NET 4.6.1 应用程序中使用 .NET Standard 2.0 包吗?

Can I use a .NET Standard 2.0 package in a classic .NET 4.6.1 application?

我有点困惑如何在 .net core 和 [=16= 之间共享 .net-standard 2 nuget 包.net-framework 个应用程序。例如,我创建了 .net-standard 2.0 包,只有 <TargetFramework>netstandard2.0</TargetFramework>。所以我在包中只有 ../libs/netstandard2.0 文件夹。我可以将它按原样安装到 .net-framework 4.6.1 中吗?申请(据compatibility matrix我可以)?或者我应该让这个包多目标<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>?这里有什么优势?

是的,的确如此。您可以仅从 .NET Framework 4.6.1 项目中引用 .NET Standard 2.0 包(较低版本的框架将无法与 .NET Standard 2.0 一起使用)。无需单独的目标或重建。

另请阅读Referencing .NET Standard Assemblies from both .NET Core and .NET Framework

关于使用多目标的好处:有 none 除非您需要。这样的情况会怎样?根据 the documentation:

You may need to target older versions of the .NET Framework when your project supports both the .NET Framework and .NET Core. In this scenario, if you want to use newer APIs and language constructs for the newer targets, use #if directives in your code. You also might need to add different packages and dependencies for each platform you're targeting to include the different APIs needed for each case.

所以只有在代码或引用不兼容时才真正需要它。如果你没有这个问题,你就不需要多目标。

有一个示例给出了如何包含基于构建目标的引用,这可能会提供一些见解:

<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
  <Reference Include="System.Net" />
</ItemGroup>