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

标签 c# .net .net-standard-2.0

我有点困惑如何在.net core.net-framework应用程序之间共享.net-standard 2 nuget包。例如,我创建了 .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 .

关于使用多目标的好处:除非需要,否则没有好处。这样的情况会是什么情况呢?根据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>

关于c# - 我可以在经典 .NET 4.6.1 应用程序中使用 .NET Standard 2.0 包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46539591/

相关文章:

c# - 当将整数视为对象并进行比较时,为什么整数不会提升为 double ?

c# - 当 .NET 462 项目引用时,.NET Standard 2.0 dll 的 transient 依赖项在 bin 中丢失

c# - .NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework

c# - 以编程方式最小化 UWP

c# - 如何快速判断列表是否仅包含重复项?

c# - 如何在数据表中选择不同的行并将其存储到数组中

c# - 如何将 ToLongDateString() 的日期和月份首字母大写导致 es-mx Culture?

c# - 如何正确实现 .net 标准库的单元测试

c# - 将具有子对象的对象作为属性绑定(bind)到数据网格

c# - 在 ServiceRoute 中使用通配符