首页 文章

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

提问于
浏览
3

我正在将旧的.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 Files \ dotnet \ sdk \ 2.1.4 \ Sdks \ Microsoft.NET.Sdk \ build \ Microsoft.NET.Sdk.DefaultItems.targets

2 回答

  • -1

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

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

    新格式的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 文件,但是,它们的分组可能会令人困惑,因为它可能会在没有任何子分组的情况下展平 . 在这种情况下,您不应在 .csproj 中明显包含任何 *.cs 文件 . 项目使用的globbing模式将自动为您包含项目中的文件 .

相关问题