Share via


使用項目函式

工作和目標中的程式碼可以呼叫項目函式,以取得專案內項目的相關資訊 (在 MSBuild 4.0 和更新版本中)。 這些函式會簡化取得 Distinct() 項目的方式,速度比執行項目迴圈還快。

字串項目函式

您可以使用 .NET Framework 中的字串方法和屬性,來操作任何項目值。 針對 String 方法,指定方法名稱。 針對 String 屬性,在 "get_" 後面指定屬性名稱。

針對有多個字串的項目,字串方法或屬性是在每個字串上執行。

下列範例示範如何使用這些字串項目函式。

<ItemGroup>
    <theItem Include="andromeda;tadpole;cartwheel" />
</ItemGroup>

<Target Name = "go">
    <Message Text="IndexOf  @(theItem->IndexOf('r'))" />
    <Message Text="Replace  @(theItem->Replace('tadpole', 'pinwheel'))" />
    <Message Text="Length   @(theItem->get_Length())" />
    <Message Text="Chars    @(theItem->get_Chars(2))" />
</Target>

  <!--
  Output:
    IndexOf  3;-1;2
    Replace  andromeda;pinwheel;cartwheel
    Length   9;7;9
    Chars    d;d;r
  -->

內建項目函式

下表列出項目可用的內建函式。

函式 範例 描述
Combine @(MyItems->Combine('path')) 傳回一組具有附加至所有輸入項目之指定相對路徑的新項目。
Count @(MyItems->Count()) 傳回項目計數。
DirectoryName @(MyItems->DirectoryName()) 傳回每個項目之 Path.DirectoryName 的對等項目。
Distinct @(MyItems->Distinct()) 傳回具有相異 Include 值的項目。 會略過中繼資料。 比較不區分大小寫。
DistinctWithCase @(MyItems->DistinctWithCase()) 傳回具有相異 itemspec 值的項目。 會略過中繼資料。 比較會區分大小寫。
Exists @(MyItems->Exists()) 將一組項目篩選為實際存在於磁碟上的項目。
GetPathsOfAllDirectoriesAbove @(MyItems->GetPathsOfAllFilesAbove()) 指定一組項目,傳回代表所有祖系目錄的項目。 不保證訂單。
Reverse @(MyItems->Reverse()) 以反向順序傳回項目。
AnyHaveMetadataValue @(MyItems->AnyHaveMetadataValue("MetadataName", "MetadataValue")) 傳回 boolean,以指出任何項目是否具有指定的中繼資料名稱和值。 比較不區分大小寫。
ClearMetadata @(MyItems->ClearMetadata()) 傳回已清除其中繼資料的項目。 只會保留 itemspec
HasMetadata @(MyItems->HasMetadata("MetadataName")) 傳回具有所指定中繼資料名稱的項目。 比較不區分大小寫。
Metadata @(MyItems->Metadata("MetadataName")) 傳回具有中繼資料名稱的中繼資料值。 傳回的項目與來源值具有相同的中繼資料。
WithMetadataValue @(MyItems->WithMetadataValue("MetadataName", "MetadataValue")) 傳回具有所指定中繼資料名稱和值的項目。 比較不區分大小寫。

注意

Exists 也可以在其他內容中使用; 在 MSBuild 條件中,例如 Condition="Exists('path')"; 或在 Static 屬性函式中,例如 $([System.IO.File]::Exists("path"))

下列範例示範如何使用內建項目函式。

<ItemGroup>
    <TheItem Include="first">
        <Plant>geranium</Plant>
    </TheItem>
    <TheItem Include="second">
        <Plant>algae</Plant>
    </TheItem>
    <TheItem Include="third">
        <Plant>geranium</Plant>
    </TheItem>
</ItemGroup>

<Target Name="go">
    <Message Text="MetaData:    @(TheItem->Metadata('Plant'))" />
    <Message Text="HasMetadata: @(theItem->HasMetadata('Plant'))" />
    <Message Text="WithMetadataValue: @(TheItem->WithMetadataValue('Plant', 'geranium'))" />
    <Message Text=" " />
    <Message Text="Count:   @(theItem->Count())" />
    <Message Text="Reverse: @(theItem->Reverse())" />
</Target>

  <!--
  Output:
    MetaData:    geranium;algae;geranium
    HasMetadata: first;second;third
    WithMetadataValue: first;third

    Count:   3
    Reverse: third;second;first
  -->

使用中繼資料項目函式時偵測重複項目

Metadata 項目函式會保留來源項目的原始中繼資料。 當考慮傳回的項目是否重複時,這有一些影響。 若要控制處理重複項目的方式,您可以使用屬性 KeepDuplicates。 您也可以藉由新增 RemoveMetadata來移除中繼資料,在此情況下,偵測重複項目時只會考慮值本身。

  <Target Name="MetadataToItem">
    <ItemGroup>
      <Sample Include="AAA" SomeItems="1;2;3" />
      <Sample Include="BBB" SomeItems="3;4;5" />
    </ItemGroup>

    <ItemGroup>
      <AllSomeItems Include="@(Sample->Metadata('SomeItems'))" KeepDuplicates="false" />
    </ItemGroup>
    <Message Text="AllSomeItems is @(AllSomeItems)" />
  </Target>

輸出如下所示:

MetadataToItem:
  AllSomeItems is 1;2;3;3;4;5

下列程式碼變更會導致成功偵測並移除重複的項目值:

    <ItemGroup>
      <AllSomeItems Include="@(Sample->Metadata('SomeItems'))" KeepDuplicates="false" RemoveMetadata="SomeItems" />
    </ItemGroup>

MSBuild 條件函式

函式 HasTrailingSlash 不是項目函式。 其可與 Condition 屬性搭配使用。 請參閱 MSBuild 條件

您也可以使用屬性對項目清單執行操作,例如篩選項目中繼資料。 如需詳細資訊,請參閱項目