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

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

问题描述

是否可以获取与给定项目关联的所有元数据键?

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

我想做类似以下的事情.

I want to do something like the following.

给出:

  <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}\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>

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

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天全站免登陆