如何获取任何 ItemGroup 项目的所有元数据键? [英] How to get all the metadata keys for any ItemGroup item?

查看:11
本文介绍了如何获取任何 ItemGroup 项目的所有元数据键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取与给定项目关联的所有元数据键?

Is there a way to get all the metadata keys associated with a given item?

我想做如下的事情.

给定:

  <ItemGroup>
    <MyItems Include="item1">
      <key1>val1</key1>
      <key2>val2</key2>
      <key3>val3</key3>
    </MyItems>
    <MyItems Include="item2">
      <key4>val4</key4>
    </MyItems>
  </ItemGroup>

能够确定 item1 具有可用于 key1、key2 和 key3 的元数据,并且 item2 具有可用于 key4 的元数据,而无需知道这些键的实际名称.

Be able to determine that item1 has metadata available for key1, key2, and key3, and that item2 has metadata available for key4, without knowing what the names of those keys actually are.

实际上,我正在尝试使用元数据来指定我不知道的属性,然后尝试找出一种方法来检查已指定的属性.

In effect, I'm trying to use the metadata to specify attributes that I have no idea about, and then trying to figure out a way to check to see what attributes have been specified.

换句话说,我相信每个项目的元数据只是一个包含键/值对的散列,我试图弄清楚所有键是什么.

Put another way, I believe the metadata of each item is just a hash containing key/value pairs and I'm trying to figure out what all the keys are.

有人知道如何用 msbuild 做到这一点吗?

Anyone know how to do this with msbuild?

谢谢

推荐答案

我最终使用类似于以下的自定义内联任务解决了这个问题:

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}
", item);
              foreach (string parameter in item.MetadataNames)
              {
                  command.AppendFormat("  {0}={1}
", parameter, item.GetMetadata(parameter));
              }
              command.AppendFormat("
");
          }
          MetadataString = command.ToString();
      ]]>
    </Code>
  </Task>
</UsingTask>

请注意,以上内容还将包括 MSBuild 自动添加到项目组中每个项目的所有默认元数据(如 FullPath、RootDir、文件名等).在我的实现中,我添加了一个额外的检查来忽略那些我不关心的元数据项

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

示例用法:

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

要在 Visual Studio 的输出"窗口中查看消息输出,您可能需要将您的 MSBuild 输出详细程度至少更改为正常".

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

这篇关于如何获取任何 ItemGroup 项目的所有元数据键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆