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

Yar*_*Yar 16 c# msbuild visual-studio

我可以编译一个.cs引用的文件PropertyGroup:

<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>
Run Code Online (Sandbox Code Playgroud)

或使用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>
Run Code Online (Sandbox Code Playgroud)

我知道使用ItemGroup应该是首选的方法,但什么时候我应该使用这些属性中的每一个?

Dav*_*tin 17

将属性组视为单个变量的集合,属性只能包含一个值.

而itemgroup类似于一个数组或集合,它可以包含零个,一个或多个值.您还可以迭代项目组,这对于您希望针对多个项目执行相同任务时通常很有用.一个常见的例子是编译许多文件.

  • ItemGroup值也可以具有多个属性,包括具有定义的"模式".属性是严格的单个字符串值. (2认同)