Choose 元素 (MSBuild)Choose element (MSBuild)

评估子元素,选择一组 ItemGroup 元素和/或 PropertyGroup 元素进行评估。Evaluates child elements to select one set of ItemGroup elements and/or PropertyGroup elements to evaluate.

<Project> <Choose> <When> <Choose> ...<Otherwise> <Choose> ...<Project> <Choose> <When> <Choose> ... <Otherwise> <Choose> ...

语法Syntax

<Choose>
    <When Condition="'StringA'=='StringB'">... </When>
    <Otherwise>... </Otherwise>
</Choose>

特性和元素Attributes and elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

无。None.

子元素Child elements

元素Element 描述Description
OtherwiseOtherwise 可选元素。Optional element.

当所有 When 元素的条件的计算结果为 false 时,指定要计算的代码块 PropertyGroupItemGroup 元素。Specifies the block of code PropertyGroup and ItemGroup elements to evaluate if the conditions of all When elements evaluate to false. Choose 元素中可能没有或只有一个 Otherwise 元素,并且它必须是最后一个元素。There may be zero or one Otherwise elements in a Choose element, and it must be the last element.
WhenWhen 必需的元素。Required element.

指定一个可能的代码块供 Choose 元素选择。Specifies a possible block of code for the Choose element to select. Choose 元素中可能有一个或多个 When 元素。There may be one or more When elements in a Choose element.

父元素Parent elements

元素Element 描述Description
OtherwiseOtherwise 当所有 When 元素的条件的计算结果为 false 时,指定要执行的代码块。Specifies the block of code to execute if the conditions of all When elements evaluate to false.
ProjectProject MSBuild 项目文件必需的根元素。Required root element of an MSBuild project file.
WhenWhen 指定一个可能的代码块供 Choose 元素选择。Specifies a possible block of code for the Choose element to select.

备注Remarks

ChooseWhenOtherwise 元素一起用来提供一种方式,通过这种方式选择代码的一部分来执行许多种可能的替代选择。The Choose, When, and Otherwise elements are used together to provide a way to select one section of code to execute out of a number of possible alternatives. 有关详细信息,请参阅条件构造For more information, see Conditional constructs.

示例Example

以下项目使用 Choose 元素来选择要在 When 元素中设置的属性值组。The following project uses the Choose element to select which set of property values in the When elements to set. 如果两个 When 元素的 Condition 属性的计算结果均为 false,则将设置 Otherwise 元素中的属性值。If the Condition attributes of both When elements evaluate to false, the property values in the Otherwise element are set.

<Project
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <PropertyGroup>
        <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
        <OutputType>Exe</OutputType>
        <RootNamespace>ConsoleApplication1</RootNamespace>
        <AssemblyName>ConsoleApplication1</AssemblyName>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <Choose>
        <When Condition=" '$(Configuration)'=='debug' ">
            <PropertyGroup>
                <DebugSymbols>true</DebugSymbols>
                <DebugType>full</DebugType>
                <Optimize>false</Optimize>
                <OutputPath>.\bin\Debug\</OutputPath>
                <DefineConstants>DEBUG;TRACE</DefineConstants>
            </PropertyGroup>
            <ItemGroup>
                <Compile Include="UnitTesting\*.cs" />
                <Reference Include="NUnit.dll" />
            </ItemGroup>
        </When>
        <When Condition=" '$(Configuration)'=='retail' ">
            <PropertyGroup>
                <DebugSymbols>false</DebugSymbols>
                <Optimize>true</Optimize>
                <OutputPath>.\bin\Release\</OutputPath>
                <DefineConstants>TRACE</DefineConstants>
            </PropertyGroup>
        </When>
        <Otherwise>
            <PropertyGroup>
                <DebugSymbols>true</DebugSymbols>
                <Optimize>false</Optimize>
                <OutputPath>.\bin\$(Configuration)\</OutputPath>
                <DefineConstants>DEBUG;TRACE</DefineConstants>
            </PropertyGroup>
        </Otherwise>
    </Choose>
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

请参阅See also