c# - TargetFramework 与 TargetFrameworks(复数)

标签 c# .net-core target-framework

.csproj在我的 .NET Core 项目中的文件中,默认有这 3 行:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

现在如果我想针对多个框架,我可以将其更改为:

<PropertyGroup>
  <TargetFrameworks>netcoreapp2.2;net4.6</TargetFrameworks>
</PropertyGroup>

差异很细微,但确实存在。针对多个框架时,您必须使用 <TargetFrameworks> (复数)而不仅仅是 <TargetFramework> (单数)。

但是为什么会这样呢?似乎只选择两者之一,然后一直使用它会更容易。这让我想到,选择不同(尽管相似)的词可能有更复杂的原因,这取决于您是否针对更多框架。任何人都可以就这个话题启发我吗?

最佳答案

基本上,当您想要针对您使用的单个框架时 <TargetFramework>标记(如果您正在构建一个以 .net-core 为目标的应用程序),但您也可以使用 <TargetFrameworks> 有条件地引用程序集多个框架。 (如果您正在为 .net 标准和 .net-core 构建应用程序)然后您可以通过使用带有 if-then-else 逻辑的预处理器符号有条件地编译这些程序集

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks>
  </PropertyGroup>

  <!-- Conditionally obtain references for the .NET Framework 4.0 target -->
  <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
    <Reference Include="System.Net" />
  </ItemGroup>

  <!-- Conditionally obtain references for the .NET Framework 4.5 target -->
  <ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Threading.Tasks" />
  </ItemGroup>

</Project>

来源:https://learn.microsoft.com/fr-fr/dotnet/standard/frameworks

关于c# - TargetFramework 与 TargetFrameworks(复数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59192074/

相关文章:

.net - 如何在同一项目的不同配置中定位不同的框架版本?

c# - Linq 获取按最快日期排序的第一行,其中 B 列不是 "Expired"或 "Cancelled"

c# - 将类转换为 XML 到字符串

javascript - 如何将页面特定的js注入(inject)_Layout

c# - IMemoryCache 保证唯一的新 key .NET-Core

触发的 Azure Function 3.0 服务总线有时会引发 Microsoft.Azure.ServiceBus.MessageLockLostException

vb.net - 无法更新 NuGet 包

c# - 通过网络发送自定义结构 - SerializationException

asp.net-core-mvc - .net core rc2 - 包依赖关系未解决

visual-studio-2010 - VS.NET 2010 中的切换目标框架