在.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/