Skip to content

Summary of Metadata of items - Sum of ItemXYZ.Value #7667

Answered by jrdodds
MeikTranel asked this question in Q&A
Discussion options

You must be logged in to vote

Are you asking how to sum the numbers, i.e. 5 + 2 + 3 = 10?

Here is a solution that uses an inline task.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ItemXYZ Include="A" Value="5" />
    <ItemXYZ Include="B" Value="2" />
    <ItemXYZ Include="C">
      <Value>3</Value>
    </ItemXYZ>
  </ItemGroup>

  <Target Name="Main">
    <Sum Addends="@(ItemXYZ->Metadata('Value'))">
      <Output TaskParameter="Total" PropertyName="Sum" />
    </Sum>
    <Message Text="Sum = $(Sum)" />
  </Target>

  <UsingTask TaskName="Sum" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <ParameterGroup>
      <Ad…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@rainersigwald
Comment options

@jrdodds
Comment options

@MeikTranel
Comment options

Answer selected by MeikTranel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants