DotNet Core .csproj代码文件作为子项

时间:2018-01-24 14:14:53

标签: c# msbuild .net-core csproj

我正在将旧的.NET Framework csproj迁移到dotnet核心。什么是dotnet核心相当于:

<Compile Include="ServiceHost.Designer.cs">
  <DependentUpon>ServiceHost.cs</DependentUpon>
</Compile>

我试过了:

<ItemGroup>
    <Compile Include="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
</ItemGroup>

但我收到了这个错误:

  

包含了重复的“编译”项目。 .NET SDK包括   默认情况下,从项目目录中“编译”项目。你也可以   从项目文件中删除这些项目,或设置   如果您愿意,可以将“EnableDefaultCompileItems”属性设置为“false”   明确地将它们包含在项目文件中。欲获得更多信息,   见https://aka.ms/sdkimplicititems。重复的项目是:   'ProjectInstaller.Designer.cs';   'ServiceHost.Designer.cs'TestWindowsService C:\ Program   文件\ DOTNET \ SDK \ 2.1.4 \ SDKS \ Microsoft.NET.Sdk \建立\ Microsoft.NET.Sdk.DefaultItems.targets

2 个答案:

答案 0 :(得分:9)

由于默认情况下包含这些项目,因此如果您只想修改需要此更新的项目而不是单独列出每个cs文件,则需要使用Update而不是Include

<ItemGroup>
    <Compile Update="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
</ItemGroup>

答案 1 :(得分:-2)

新格式的SDK项目默认将几个globbing模式设置为“True”。其中之一是将所有* .cs文件包含在项目目录及其子目录中。您获得的错误是由于双重包含* .cs文件引起的,并且有一种简单的方法可以防止它出现在错误消息中。您应该在项目中包含以下属性:

<PropertyGroup>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

使用该设置,您必须使用以下命令显式包含项目中的所有文件:

<ItemGroup>
    <Compile Include="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
    <Compile Include="MyOtherFile.cs"/>
    .......
</ItemGroup>

如果您决定不使用EnableDefaultCompileItems设置,则会自动包含所有*.cs个文件,但是,它们的分组可能会造成混淆,因为它可能会在没有任何子分组的情况下展平。在这种情况下,您不应在*.cs中明确包含任何.csproj个文件。项目使用的globbing模式将自动为您包含项目中的文件。

相关问题