文件是否可以嵌套在用于.NET 核心(非 ASP.NET 核心)项目的 VS2017解决方案资源管理器中?

在“老式”MSBuild 项目中——例如仍然被 Windows Forms 在 VS2017中使用——文件可以通过 csproj 文件中的 DependentUpon项“嵌套”。

我在 Noda Time 中使用这种方法将单元测试分组在一起,例如。

<Compile Include="LocalDateTest.PeriodArithmetic.cs">
<DependentUpon>LocalDateTest.cs</DependentUpon>
</Compile>

这导致了易于导航的测试:

Nested tests

当我移动到 project.json时,我故意“丢失”了这个特性。NET 核心,但希望它会返回时转换为 MSBuild。但是,MSBuild 项目似乎是基于。NET Core SDK (根元素 <Project Sdk="Microsoft.NET.Sdk">)在 Visual Studio 2017中没有得到相同的处理,即使使用与“旧学校”项目相同的元素手动添加 ItemGroup

NET 核心项目为小型化的 CSS 和 Javascript 接受自动嵌套,但是不清楚如何将其应用到 C # in 中。NET 核心库项目。

11488 次浏览

I have it working in one of my Microsoft.NET.Sdk-style projects using something similar to the following:

<ItemGroup>
<Compile Update="LocalDateTest.*.cs">
<DependentUpon>LocalDateTest.cs</DependentUpon>
</Compile>
</ItemGroup>

The trick here is to use Update instead of Include. This is because the implicit items are coming from a props file that is imported before the main project. An additional Include won't affect files that are already included, but they can be modified using Update.

If you use the same prefix it will nest files automatically.

Example:

AsemblyInfo.cs
AsemblyInfo.local.cs

If you using .netstandardx.x you can not use NestedIn . It's not working.

You can do that manually in your .csproj

<ItemGroup><Compile Include="BaseClass">ChildClass.cs</Compile></ItemGroup>

In Visual Studio 2019, I have a .NET Core 2.2 <Project Sdk="Microsoft.NET.Sdk"> project in which I wanted the nicely-nested appsettings.json / appsettings.Development.json files, just like they do automatically for <Project Sdk="Microsoft.NET.Sdk.Web"> projects.

Here's what I had to add to the .CSPROJ:

  <ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="appsettings.Development.json">
<DependentUpon>appsettings.json</DependentUpon>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

After that, I had to unload/reload the project for the change to take effect in Solution Explorer. Note that I also set these files to always be copied to the output directory.

If you want to wildcard it instead of hand-crafting each entry, adding these lines to your .csproj file means that anything like Foo.tests.cs automagically gets nested under Foo.cs

Tested and working in VS2019 with .NET Core 3.1.0

  <ItemGroup>
<Compile Update="**\*.tests.cs">
<DependentUpon>$([System.String]::Copy(%(Filename)).Replace(".tests",".cs"))</DependentUpon>
</Compile>
</ItemGroup>