Otherwise 元素 (MSBuild)

指定只有當所有 When 項目的條件評估為 false 時,才需執行的程式碼區塊。

<專案><選擇><當><選擇> ...<否則><選擇> ...

語法

<Otherwise>
    <PropertyGroup>... </PropertyGroup>
    <ItemGroup>... </ItemGroup>
    <Choose>... </Choose>
</Otherwise>

屬性和元素

下列章節說明屬性、子元素和父元素。

屬性

無。

子元素

元素 描述
選擇 選擇性項目。

評估子元素,以選取要執行的一個程式碼區段。 Otherwise 元素中可能有零個或多個 Choose 元素。
ItemGroup 選擇性項目。

包含一組使用者定義的 Item 項目。 Otherwise 元素中可能有零個或多個 ItemGroup 元素。
PropertyGroup 選擇性項目。

包含一組使用者定義的 Property 項目。 Otherwise 元素中可能有零個或多個 PropertyGroup 元素。

父元素

元素 描述
選擇 評估子元素,以選取要執行的一個程式碼區段。

備註

Choose 項目中可能只有一個 Otherwise 項目,而且它必須是最後一個項目。

ChooseWhenOtherwise 元素會一起用來提供選取一個程式碼區段的方式,以執行一些可能的替代方案。 如需詳細資訊,請參閱條件式建構

範例

下列專案使用 Choose 元素來選取 When 元素中要設定的屬性值集合。 如果兩個 When 元素的 Condition 屬性都評估為 false,則 Otherwise 元素中的屬性值已設定。

<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>

另請參閱