Created
July 22, 2025 12:47
-
-
Save olivier-spinelli/779cfdfad55e4a5f38488ea536343fb4 to your computer and use it in GitHub Desktop.
Dump ItemGroup content with meta data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- From https://stackoverflow.com/questions/17461175/how-to-get-all-the-metadata-keys-for-any-itemgroup-item --> | |
<UsingTask TaskName="GetMetadataTask" | |
TaskFactory="RoslynCodeTaskFactory" | |
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" > | |
<ParameterGroup> | |
<MyItemGroup ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> | |
<MetadataString Output="true" /> | |
</ParameterGroup> | |
<Task> | |
<Using Namespace="System"/> | |
<Code Type="Fragment" Language="cs"> | |
<![CDATA[ | |
StringBuilder command = new StringBuilder(); | |
foreach (ITaskItem item in MyItemGroup ) | |
{ | |
command.AppendFormat("ItemName={0}\r\n", item); | |
foreach (string parameter in item.MetadataNames) | |
{ | |
command.AppendFormat(" {0}={1}\r\n", parameter, item.GetMetadata(parameter)); | |
} | |
command.AppendFormat("\r\n"); | |
} | |
MetadataString = command.ToString(); | |
]]> | |
</Code> | |
</Task> | |
</UsingTask> | |
<!-- Usage (here for Reference). --> | |
<Target Name="PrintReferencesItemGroup" AfterTargets="ResolveReferences"> | |
<GetMetadataTask MyItemGroup="@(Reference)"> | |
<Output TaskParameter="MetadataString" PropertyName="YourMetadataString"/> | |
</GetMetadataTask> | |
<Message Text="$(YourMetadataString)" Importance="High" /> | |
</Target> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment