Export (0) Print
Expand All

Item Functions

Updated: September 2012

Starting with MSBuild 4.0, code in tasks and targets can call item functions to get information about the items in the project. These functions simplify getting Distinct() items and are faster than looping through the items.

You can use string methods and properties in the .NET Framework to operate on any item value. For String methods, specify the method name. For String properties, specify the property name after "get_".

For items that have multiple strings, the string method or property runs on each string.

The following example shows how to use these string item functions.

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

The table below lists the intrinsic functions available for items.

Function

Example

Description

DirectoryName

@(MyItem->DirectoryName())

Returns the equivalent of Path.DirectoryName for each item.

Distinct

@(MyItem->Distinct())

Returns items that have distinct Include values. Metadata is ignored. The comparison is case insensitive.

DistinctWithCase

@(MyItem->DistinctWithCase())

Returns items that have distinct itemspec values. Metadata is ignored. The comparison is case sensitive.

Reverse

@(MyItem->Reverse())

Returns the items in reverse order.

AnyHaveMetadataValue

@(MyItem->AnyHaveMetadataValue("MetadataName", "MetadataValue"))

Returns a boolean to indicate whether any item has the given metadata name and value. The comparison is case insensitive.

ClearMetadata

@(MyItem->ClearMetadata())

Returns items with their metadata cleared. Only the itemspec is retained.

Metadata

@(MyItem->Metadata("MetadataName"))

Returns the values of the metadata that have the metadata name.

WithMetadataValue

@(MyItem->WithMetadataValue("MetadataName", "MetadataValue")

Returns items that have the given metadata name and value. The comparison is case insensitive.

The following example shows how to use intrinsic item functions.

<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="WithMetadataValue: @(TheItem->WithMetadataValue('Plant', 'geranium'))" />
    <Message Text=" " />
    <Message Text="Reverse: @(theItem->Reverse())" />
</Target>

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

    Reverse: third;second;first
  -->

Date

History

Reason

September 2012

Improved the descriptions for the intrinsic item functions.

Information enhancement.

September 2012

Fixed information about the AnyHaveMetadataValue function.

Content bug fix.

August 2012

Added an example of string item functions.

Information enhancement.

May 2012

Added an example of intrinsic item functions.

Information enhancement.

May 2012

Fixed information about the WithMetadataValue function.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft