MSBuild Project File - Items
Items are the stuff you are sending into the build system. Usually, they are grouped together into what is called an "Item Collection". You will pass these collections to the tasks defined later in the project file.
You specify an item collection as:
<ItemGroup>
…
</ItemGroup>
Each item is specified as an element of this group. Each element is named the same (this is what makes them a collection). However, each element as an Include attribute, which specifies a file to include. The following is the ItemGroup Specification from our ConsoleApp.csproj file:
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
In this instance, we have a collection called "Compile", that includes two files, "Program.cs" and "Properties\AssemblyInfo.cs".
To reference this item collection in the project file (i.e., pass the collection to a task), you use the syntax @(NameOfItemCollection). So, for our example, you would use:
@(Compile)
Item collections support the use of wild cards. They also support the use of an exclude attribute, allowing you to exclude specific files from the collection:
<ItemGroup>
<MyTextFiles Include="*.txt" Exclude="DoNotUseMe.txt"/>
</ItemGroup>
In the above Item Group, we are using the wildcard "*" to say add every .txt file in the same directory as the MSBuild Project File, except the file names DoNotUseMe.txt.
Finally, you can include "metadata" for each item. This metadata can be used by tasks and targets. You declare the metadata as a child element of the item:
<ItemGroup>
<MyFile Include="myfile.cs">
<MetaData1>Some Data</MetaData1>
<MetaData2>Some More Data</MetaData2>
</MyFile>
</ItemGroup>
Notice how you need a closing element for each item now, as it has child elements contained in it. To reference this meta data, you use the %(ItemMetaDataName):
%(MetaData1) or %(MyFile.MetaData1)
You can have multiple Item Groups in your project file. So, given all this information, let's examine our ConsoleApp.csproj project file. It contains two Item Collections:
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
The first Item Collection is called "Reference" and contains three entries. These are the "using" statements from the Program.cs file. The second Item Collection is called "Compile" and contains the two .cs files that make up the project.
Reference: