How to get all the metadata keys for any ItemGroup item?

孤人 提交于 2019-11-28 23:58:17
Zain Rizvi

I ended up solving this using a custom inline task similar to the following:

<UsingTask
  TaskName="GetMetadataTask"
  TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.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>

Note that the above will also include all the default metadata that MSBuild automatically adds to every item in an itemgroup (like FullPath, RootDir, Filename, etc). In my implementation I added an extra check to ignore those metadata items that I didn't care about

Sample usage:

<GetMetadataTask MyItemGroup="@(YourItemGroup)">
  <Output TaskParameter="MetadataString" PropertyName="YourMetadataString"/>
</GetMetadataTask>
<Message Text="$(YourMetadataString)" />

To see message output in Visual Studio's Output window, you may need to change your MSBuild output verbosity to at least Normal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!