在 msbuild 条件下针对 .net5 或更高版本的所有 .Net 版本

问题描述 投票:0回答:2

使用多个目标框架 .Net 6.0 和 .Net Framework 4.7.2 (

<TargetFrameworks>net6.0;net472</TargetFrameworks>
) 应用程序构建应用程序时,您可以在项目或目标文件中指定 msbuild 的条件,如下所示:

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> Something </ItemGroup>
OR
<Target Name="MyUniqueTarget213243" BeforeTargets="Build">
    <Message Importance="high" Text="Message for .net Framework" Condition="$(TargetFramework)' == 'net472'" />
    <Message Importance="high" Text="Message for .net 6 and higher" Condition="$(TargetFramework)' == 'net6.0'" />
</Target>

这适用于 .net Framework 4.7.2 和 .Net 6.0 的特定版本,但对于 .net 8 或 .net Framework 3.5 会出现问题。

由于旧框架始终是 net472,所以没问题,但新框架会随时更新到 8.0 或 10.0 或其他版本。

我可以设置一个条件来定位 .Net 5 及更高版本的所有版本(例如 net5.0、net6.0、net8.0 或 net8.3)[如果会有 8.3],但不是 .net core( netcoreapp2.1) 或 .net Framework 4.7.2(net472)

到目前为止我尝试过的:

<Message Importance="high" Text="Output Substring+Trim: '$(TargetFramework.Substring(3).Trim(`1234567890`))'" />
<Message Condition="'$(TargetFramework.Substring(3).Trim(`1234567890`))' == '.'" Importance="high" Text="ShowsIfConditionIsTrue" />
<!---> error MSB4184: The expression """.Substring(3)" cannot be evaluated. startIndex cannot be larger than length of string. Msbuild seems to do a build where TargetFramework is ''-->
<!---> Can be paired with condition like '$(TargetFramework)' != '', but complicated and seems stupid-->
<Message Importance="high" Text="Output Trim: '$(TargetFramework.Trim(`net1234567890`))'" />
<Message Condition="'$(TargetFramework.Trim(`net1234567890`))' == '.'" Importance="high" Text="ShowsIfConditionIsTrue" />
<!---> unreliable-->
<Message Condition="'$(TargetFramework.Contains(`net6`))' == true" Importance="high" Text="ShowsIfConditionIsTrue" />
<!---> checks only one framework-->
<Message Condition="'$(new string[]{`net5.0`, `net6.0`}.Contains(TargetFramework))' == true" Importance="high" Text="ShowsIfConditionIsTrue" />
<!---> error: new string[]{`net5.0`, `net6.0`}.Contains(TargetFramework) cannot be evaluated-->
<!--  -> only fixed values-->
<Message Condition="'$(TargetFramework)' != 'net472'" Importance="high" Text="ShowsIfConditionIsTrue" />
<!---> may target net45 or netcoreSomething or whatever-->
.net msbuild
2个回答
0
投票

.NET 6 基本上是向前兼容的:为 .NET 6 编译的二进制文件将在 .NET 8 运行时上运行*。您不必明确以 .NET 8 为目标来支持它。

例如,

Newtonsoft.Json
仅显式支持 .NET 6(csproj),但它仍然可以在 .NET 8 上运行,如 NuGet 上所示。

*:如果二进制文件受到 .NET 8 和 .NET 7 中大量二进制文件破坏性更改的影响,它可能无法运行。不过可能性很小。


0
投票

根据 Xenohon 的评论,这样的事情怎么样:

<PropertyGroup>
    <IsUnifiedNETVersion>$([MSBuild]::VersionGreaterThanOrEquals('$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)'))','6.0'))</IsUnifiedNETVersion>
    <MessageText Condition="$(IsUnifiedNETVersion) == 'true'">NET 6 or Higher is beeing used</MessageText>
    <MessageText Condition="$(IsUnifiedNETVersion) == 'false'">NET472 is beeing used</MessageText>
</PropertyGroup>

<Target Name="MyTarget" AfterTargets="AfterBuild">
    <Message Importance="High" Text="IsNet6OrHigher = $(IsUnifiedNETVersion)" />
    <Message Importance="High" Text="$(MessageText)" />
</Target>
© www.soinside.com 2019 - 2024. All rights reserved.