MSBuild 条件MSBuild conditions

MSBuild 支持一组特定的条件,只要允许使用 Condition 属性,便应用这些条件。MSBuild supports a specific set of conditions that can be applied wherever a Condition attribute is allowed. 下表对这些条件进行了说明。The following table explains those conditions.

条件Condition 描述Description
'stringA' == 'stringB''stringA' == 'stringB' 如果 stringA 等于 stringB,则计算结果为 trueEvaluates to true if stringA equals stringB.

例如:For example:

Condition="'$(Configuration)'=='DEBUG'"

对于简单的字母数字字符串或布尔值,不需要单引号。Single quotes are not required for simple alphanumeric strings or boolean values. 但对于空值,单引号是必需的。However, single quotes are required for empty values. 此检查不区分大小写。This check is case insensitive.
'stringA' != 'stringB''stringA' != 'stringB' 如果 stringA 不等于 stringB,则计算结果为 trueEvaluates to true if stringA is not equal to stringB.

例如:For example:

Condition="'$(Configuration)'!='DEBUG'"

对于简单的字母数字字符串或布尔值,不需要单引号。Single quotes are not required for simple alphanumeric strings or boolean values. 但对于空值,单引号是必需的。However, single quotes are required for empty values. 此检查不区分大小写。This check is case insensitive.
<, >, <=, >=<, >, <=, >= 计算操作数的数值。Evaluates the numeric values of the operands. 如果关系评估为 true,则返回 trueReturns true if the relational evaluation is true. 操作数的计算结果必须为十进制或十六进制数。Operands must evaluate to a decimal or hexadecimal number. 十六进制数必须以“0x”开头。Hexadecimal numbers must begin with "0x". 注意: 在 XML 中,必须对字符 <> 进行转义。Note: In XML, the characters < and > must be escaped. 符号 < 表示为 &lt;The symbol < is represented as &lt;. 符号 > 表示为 &gt;The symbol > is represented as &gt;.
Exists('stringA')Exists('stringA') 如果存在名为 stringA 的文件或文件夹,则计算结果为 trueEvaluates to true if a file or folder with the name stringA exists.

例如:For example:

Condition="!Exists('$(Folder)')"

对于简单的字母数字字符串或布尔值,不需要单引号。Single quotes are not required for simple alphanumeric strings or boolean values. 但对于空值,单引号是必需的。However, single quotes are required for empty values.
HasTrailingSlash('stringA')HasTrailingSlash('stringA') 如果指定的字符串末尾包含反斜杠 (\) 或正斜杠 (/) 字符,则计算结果为 trueEvaluates to true if the specified string contains either a trailing backward slash (\) or forward slash (/) character.

例如:For example:

Condition="!HasTrailingSlash('$(OutputPath)')"

对于简单的字母数字字符串或布尔值,不需要单引号。Single quotes are not required for simple alphanumeric strings or boolean values. 但对于空值,单引号是必需的。However, single quotes are required for empty values.
!! 如果操作数计算结果为 false,则计算结果为 trueEvaluates to true if the operand evaluates to false.
And 如果两个操作数计算结果均为 true,则计算结果为 trueEvaluates to true if both operands evaluate to true.
Or 如果至少一个操作数计算结果为 true,则计算结果为 trueEvaluates to true if at least one of the operands evaluates to true.
()() 如果内含表达式计算结果为 true,则分组机制的计算结果为 trueGrouping mechanism that evaluates to true if expressions contained inside evaluate to true.
$if$ ( %expression% ), $else$, $endif$$if$ ( %expression% ), $else$, $endif$ 检查指定的 %expression% 是否与传递的自定义模板参数的字符串值相匹配。Checks whether the specified %expression% matches the string value of the passed custom template parameter. 如果 $if$ 条件计算结果为 true,则其语句处于运行状态;否则,检查 $else$ 条件。If the $if$ condition evaluates to true, then its statements are run; otherwise, the $else$ condition is checked. 如果 $else$ 条件为 true,则其语句为运行状态;否则,$endif$ 条件将结束表达式求值。If the $else$ condition is true, then its statements are run; otherwise, the $endif$ condition ends expression evaluation.

有关用法的示例,请参阅 Visual Studio Project/Item Template Parameter Logic(Visual Studio 项目/项模板参数逻辑)。For examples of usage, see Visual Studio project/item template parameter logic.

在以下示例所示的情况下可以使用字符串方法,其中 TrimEnd() 函数仅用于比较字符串的相关部分,以区分 .NET Framework 与 .NET Core 目标框架。You can use string methods in conditions, as shown in the following example, in which the TrimEnd() function is used to compare only the relevant part of the string, to differentiate between .NET Framework and .NET Core target frameworks.

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

    <PropertyGroup>
        <TargetFrameworks>net45;net48;netstandard2.1;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
    </PropertyGroup>

    <PropertyGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
        <!-- Properties for .NET Framework -->
    </PropertyGroup>

</Project>

在 MSBuild 项目文件中,没有真正的布尔类型。In MSBuild project files, there's no true Boolean type. 布尔数据用属性表示,这些属性可能是空的,也可能设置为任何值。Boolean data is represented in properties that might be empty or set to any value. 因此,'$(Prop)' == 'true' 表示“如果 Prop 是 true,但 '$(Prop)' != 'false' 表示“如果 Prop 是 true 或者未设置,或者设置为其他内容。”Therefore, '$(Prop)' == 'true' means "if Prop is true," but '$(Prop)' != 'false' means "if Prop is true or unset or set to something else."

布尔逻辑只在条件的上下文中计算,因此,像 <Prop2>'$(Prop1)' == 'true'</Prop> 这样的属性设置是以(变量扩展后的)字符串的形式来表示的,而不是以布尔值的形式来计算。Boolean logic is only evaluated in the context of conditions, so property settings such as <Prop2>'$(Prop1)' == 'true'</Prop> are represented as a string (after variable expansion), not evaluated as Boolean values.

MSBuild 实现了一些特殊的处理规则,使其更容易处理用作布尔值的字符串属性。MSBuild implements a few special processing rules to make it easier to work with string properties that are used as Boolean values. 接受布尔文本,因此 Condition="true"Condition="false" 按预期方式工作。Boolean literals are accepted, so Condition="true" and Condition="false" work as expected. MSBuild 还包括支持布尔求反运算符的特殊规则。MSBuild also includes special rules to support the Boolean negation operator. 因此,如果 $(Prop) 为“true”,则 !$(Prop) 展开为 !true,这等于 false,与您预期的相同。So, if $(Prop) is 'true', !$(Prop) expands to !true and this compares equal to false, as you would expect.

请参阅See also