简体   繁体   English

在MsBuild中,PropertyGroup和ItemGroup之间有什么区别

[英]In MsBuild, what's the difference between PropertyGroup and ItemGroup

I can compile a .cs file referenced by PropertyGroup : 我可以编译PropertyGroup引用的.cs文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
        <Compile>helloConfig.cs</Compile>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="$(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>        
</Project>

or do the same thing using ItemGroup: 或使用ItemGroup做同样的事情:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    
    <ItemGroup>
        <Compile Include="helloConfig.cs" />
    </ItemGroup>

    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>  
</Project>

I know that using ItemGroup should be the preferred method, but what's when I should use each one of these properties? 我知道使用ItemGroup应该是首选方法,但什么时候我应该使用这些属性中的每一个?

Think of property group as a collection of individual variables, properties can only hold one value. 将属性组视为单个变量的集合,属性只能包含一个值。

Whereas itemgroup is similar to an array or collection which can hold zero, one or many values. 而itemgroup类似于一个数组或集合,它可以包含零个,一个或多个值。 You can also iterate item groups which is often useful when you want to carry out the same task against multiple items. 您还可以迭代项目组,这对于您希望针对多个项目执行相同任务时通常很有用。 A common example of this is compiling many files. 一个常见的例子是编译许多文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM