CreateItem 与 ItemGroup [英] CreateItem vs ItemGroup

查看:10
本文介绍了CreateItem 与 ItemGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这样的目标内创建项目有什么区别:

What is the difference between creating an item inside a target like this:

<Target Name="DoStuff">
    <CreateItem Include="@(IntermediateAssembly)" >
        <Output TaskParameter="Include" ItemName="FileWrites"/>
    </CreateItem>
</Target>

像这样:

<Target Name="DoStuff">
    <ItemGroup>
        <FileWrites Include="@(IntermediateAssembly)" />
    </ItemGroup>
</Target>

您什么时候会使用其中一种,为什么?

When would you use one or the other and why?

推荐答案

在 3.5 之前的 MSBuild 版本中,您无法在目标内部定义属性或项目(就像在第二个示例中一样).所以使用了一个任务(CreateItem 和 CreateProperty)

In versions of MSBuild prior to 3.5 you could not define properties or items inside of targets (like in your second example). So a task was used instead (CreateItem and CreateProperty)

如果您使用的是 ToolsVersion 3.5,那么您就不需要再使用 CreateItem(不过如果您愿意,您仍然可以使用).

If you are using ToolsVersion 3.5 then you don't need to use CreateItem anymore (though you still can if you prefer).

最后,他们都创建了相同的项目,具有相同的范围.使用第二种语法更具可读性,并且设置自定义元数据更容易(在我看来).

In the end they both create the item the same, with the same scope. Using the second syntax is more readable and setting up custom meta data is much easier (in my opinion).

注意:3.5 版本的 MSBuild 随 .NET 3.5 一起安装.虽然您需要在 MSBuild 文件的 Project 标记中定义 ToolsVersion="3.5" 以使用 3.5 功能.

NOTE: The 3.5 version of MSBuild is installed with .NET 3.5. Though you need to define ToolsVersion="3.5" in the Project tag of your MSBuild file to use 3.5 features.

如果您想知道,我从 Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build 一书中获得了大部分信息,我非常喜欢这本书(但不隶属于任何方式).

In case you are wondering, I got most of this info from the book Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build which I really liked (but am not affiliated with in any way).

这篇关于CreateItem 与 ItemGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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