MsBuild StartsWith 未评估条件?

MsBuild StartsWith not Evaluating In Condition?

在尝试通过 VisualStudio 在 Mac 上设置一些代码覆盖工具并处理一个新的 Xamarin.Forms 项目时,我尝试按照 [=] 中的建议使用 netfx.props 22=]。 (我会直接评论这个问题,但是我没有足够的声望点数。)

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- When compiling .NET SDK 2.0 projects targeting .NET 4.x on Mono using 'dotnet build' you -->
    <!-- have to teach MSBuild where the Mono copy of the reference asssemblies is -->
    <TargetIsMono Condition="($(TargetFramework.StartsWith('net4')) OR $(TargetFrameworkVersion.StartsWith('v4.'))) AND '$(OS)' == 'Unix'">true</TargetIsMono>

    <!-- Look in the standard install locations -->
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/Library/Frameworks/Mono.framework/Versions/Current/lib/mono')">/Library/Frameworks/Mono.framework/Versions/Current/lib/mono</BaseFrameworkPathOverrideForMono>
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/lib/mono')">/usr/lib/mono</BaseFrameworkPathOverrideForMono>
    <BaseFrameworkPathOverrideForMono Condition="'$(BaseFrameworkPathOverrideForMono)' == '' AND '$(TargetIsMono)' == 'true' AND EXISTS('/usr/local/lib/mono')">/usr/local/lib/mono</BaseFrameworkPathOverrideForMono>

    <!-- If we found Mono reference assemblies, then use them -->
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net45' OR '$(TargetFrameworkVersion)' == 'v4.5')">$(BaseFrameworkPathOverrideForMono)/4.5-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net451' OR '$(TargetFrameworkVersion)' == 'v4.5.1')">$(BaseFrameworkPathOverrideForMono)/4.5.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net452' OR '$(TargetFrameworkVersion)' == 'v4.5.2')">$(BaseFrameworkPathOverrideForMono)/4.5.2-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net46' OR '$(TargetFrameworkVersion)' == 'v4.6')">$(BaseFrameworkPathOverrideForMono)/4.6-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net461' OR '$(TargetFrameworkVersion)' == 'v4.6.1')">$(BaseFrameworkPathOverrideForMono)/4.6.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net462' OR '$(TargetFrameworkVersion)' == 'v4.6.2')">$(BaseFrameworkPathOverrideForMono)/4.6.2-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net47' OR '$(TargetFrameworkVersion)' == 'v4.7')">$(BaseFrameworkPathOverrideForMono)/4.7-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net471' OR '$(TargetFrameworkVersion)' == 'v4.7.1')">$(BaseFrameworkPathOverrideForMono)/4.7.1-api</FrameworkPathOverride>
    <FrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != '' AND ('$(TargetFramework)' == 'net472' OR '$(TargetFrameworkVersion)' == 'v4.7.2')">$(BaseFrameworkPathOverrideForMono)/4.7.2-api</FrameworkPathOverride>
    <EnableFrameworkPathOverride Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">true</EnableFrameworkPathOverride>

    <!-- Add the Facades directory.  Not sure how else to do this. Necessary at least for .NET 4.5 -->
    <AssemblySearchPaths Condition="'$(BaseFrameworkPathOverrideForMono)' != ''">$(FrameworkPathOverride)/Facades;$(AssemblySearchPaths)</AssemblySearchPaths>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)'== 'net472' OR '$(TargetFrameworkVersion)' == 'v4.7.2'">
    <Reference Include="netstandard" />
  </ItemGroup>
</Project>

该文件已通过 .csproj 正常导入,但它仍然无法正常工作,因此我使用以下方法打印了一些调试消息:

    <Target Name="test">
        <Message Importance="High" Text="TargetFramework value is '$(TargetFramework)', starts with net4 is '$(TargetFramework.StartsWith('net4'))'" />
        <Message Importance="High" Text="TargetFrameworkVersion value is '$(TargetFrameworkVersion)', starts with v4 is '$(TargetFrameworkVersion.StartsWith('v4'))'" />
        <Message Importance="High" Text="OS value is '$(OS)'" />
        <Message Importance="High" Text="TargetIsMono value is '$(TargetIsMono)'" />
        <Message Importance="High" Text="BaseFrameworkPathOverrideForMono value is '$(BaseFrameworkPathOverrideForMono)'" />
        <Message Importance="High" Text="FrameworkPathOverride value is '$(FrameworkPathOverride)'" />
        <Message Importance="High" Text="EnableFrameworkPathOverride value is '$(EnableFrameworkPathOverride)'" />
    </Target>

并看到:

  TargetFramework value is '', starts with net4 is 'False'
  TargetFrameworkVersion value is 'v4.7', starts with v4 is 'True'
  OS value is 'Unix'
  TargetIsMono value is ''
  BaseFrameworkPathOverrideForMono value is ''
  FrameworkPathOverride value is '/Library/Frameworks/Mono.framework/Versions/6.6.0/lib/mono/4.7-api'
  EnableFrameworkPathOverride value is ''

TargetIsMono 仍然是空的,即使它应该被设置为 'true' 因为 TargetFrameworkVersion.StartsWithOS。在使用 TargetIsMono Condition 之后,问题似乎是即使 StartsWith 根据打印消息具有正确的布尔值,但它们实际上并未在 Condition 中正确评估。

    <TargetIsMono Condition="$(TargetFrameworkVersion.StartsWith('v4.'))>true</TargetIsMono>

没有将 TargetIsMono 设置为 'true',尽管它应该设置。

有人有什么想法吗?我是 MsBuild、Xamarin.Forms 和 VisualStudio 的新手。如有任何帮助,我们将不胜感激!

我将目标框架版本切换到 4.8,然后又切换回 4.7,这自行解决了。 ¯\_(ツ)_/¯