Bagikan melalui


Elemen When (MSBuild)

Menentukan kemungkinan blok kode untuk elemen Choose yang akan dipilih.

<Proyek><Pilih><When><Pilih> ... <Atau><Pilih> ...

Sintaks

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

Atribut dan elemen

Bagian berikut menjelaskan atribut, elemen turunan, dan elemen induk.

Atribut

Atribut Deskripsi
Kondisi Atribut yang diperlukan.

Kondisi untuk dievaluasi. Untuk informasi selengkapnya, lihat Syarat-syarat.

Elemen anak

Elemen Deskripsi
Pilih Elemen opsional.

Mengevaluasi elemen turunan untuk memilih satu bagian kode yang akan dieksekusi. Mungkin ada nol atau lebih elemen Choose dalam elemen When.
ItemGroup Elemen opsional.

Berisi serangkaian elemen Item yang ditentukan pengguna. Mungkin ada nol atau lebih elemen ItemGroup dalam elemen When.
PropertyGroup Elemen opsional.

Berisi sekumpulan elemen Properti yang ditentukan pengguna. Mungkin ada nol atau lebih elemen PropertyGroup dalam elemen When.

Elemen induk

Elemen Deskripsi
Memilih elemen (MSBuild) Mengevaluasi elemen turunan untuk memilih satu bagian kode yang akan dieksekusi.

Keterangan

Jika atribut Condition bernilai true, elemen ItemGroup dan PropertyGroup turunan dari elemen When akan dieksekusi dan semua elemen When berikutnya dilewati.

Elemen Choose, When, dan Otherwise digunakan bersama-sama untuk menyediakan cara untuk memilih satu bagian kode untuk dijalankan dari sejumlah alternatif yang mungkin. Untuk informasi selengkapnya, lihat Konstruksi kondisional.

Contoh

Proyek berikut menggunakan elemen Choose untuk memilih kumpulan nilai properti mana dalam elemen When yang akan ditetapkan. Jika atribut Condition dari kedua elemen When dievaluasi menjadi false, nilai properti di elemen Otherwise ditetapkan. Saat menjalankan contoh, coba lewati berbagai pengaturan properti dari baris perintah, seperti msbuild myproj.proj -p:Configuration=Test;Platform=x86, dan lihat seperti apa jalur outputnya. Contohnya mengandaikan persyaratannya adalah mengatur properti tertentu untuk build debug dan rilis, termasuk folder output berdasarkan bitness platform daripada nama platform yang sebenarnya, dan juga mendukung konfigurasi 'Uji' dan 'Ritel', tetapi perlakukan 'Ritel' sebagai 'Rilis'.

<Project>
    <PropertyGroup>
       <Configuration Condition="$(Configuration) == ''">Debug</Configuration>
       <Platform Condition="$(Platform) == ''">x64</Platform>
    </PropertyGroup>

  <Choose>
     <When Condition="$(Configuration)=='Test'">
        <PropertyGroup>
            <DebugSymbols>true</DebugSymbols>
            <DebugType>full</DebugType>
            <Optimize>false</Optimize>
            <DefineConstants>DEBUG;TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'">
            <PropertyGroup>
                <OutputPath>.\bin\Test\32-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'">
            <PropertyGroup>
                <OutputPath>.\bin\Test\64-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <!-- For any other platform, use the platform name -->
          <Otherwise>
            <PropertyGroup>
              <OutputPath>.\bin\Test\$(Platform)\</OutputPath>
            </PropertyGroup>
          </Otherwise>
        </Choose>
      </When>
      <When Condition="$(Configuration)=='Retail' Or $(Configuration)=='Release'">
        <PropertyGroup>
            <DebugSymbols>false</DebugSymbols>
            <Optimize>true</Optimize>
            <DefineConstants>TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'">
             <PropertyGroup>
                <OutputPath>.\bin\Release\32-bit\</OutputPath>
             </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'">
             <PropertyGroup>
                <OutputPath>.\bin\Release\64-bit\</OutputPath>
             </PropertyGroup>
          </When>
          <!-- For any other platform, use the platform name -->
          <Otherwise>
            <PropertyGroup>
                <OutputPath>.\bin\Release\$(Platform)\</OutputPath>
            </PropertyGroup>
          </Otherwise>
        </Choose>
      </When>
      <!-- For any other configuration, use debug properties-->
      <Otherwise>
        <PropertyGroup>
            <DebugSymbols>true</DebugSymbols>
            <DebugType>full</DebugType>
            <Optimize>false</Optimize>
            <DefineConstants>DEBUG;TRACE</DefineConstants>
        </PropertyGroup>
        <Choose>
          <When Condition="$(Platform)=='x86' Or $(Platform)=='ARM32'">
            <PropertyGroup>
              <OutputPath>.\bin\$(Configuration)\32-bit\</OutputPath>
            </PropertyGroup>
          </When>
          <When Condition="$(Platform)=='x64' Or $(Platform)=='ARM64'">
            <PropertyGroup>
              <OutputPath>.\bin\$(Configuration)\64-bit\</OutputPath>
            </PropertyGroup>
          </When>
        </Choose>
       </Otherwise>
  </Choose>

  <Target Name="ShowProperties">
    <Message Text="DebugSymbols: $(DebugSymbols)"/>
    <Message Text="Optimize: $(Optimize)"/>
    <Message Text="DefineConstants: $(DefineConstants)"/>
    <Message Text="OutputPath: $(OutputPath)"/>
  </Target>
</Project>

Lihat juga