Search code examples
c#msbuildvisual-studio-2022

Visual Studio 2022 - Nesting partial classes (files)


I noticed that, in my solution, nesting partial classes is only working for Microsoft.NET.Sdk.Web project.
For the rest of the projects (Microsoft.NET.Sdk) it is not working.
Does anyone know anything about this?


Edit: I posted the SDK because I think that is the reason. I created a new solution, Console App (Microsoft.NET.Sdk) and tried creating partial classes, still doesn't nest the files.

enter image description here

enter image description here


Edit 2
After adding the following to the .csproj file, it is now nested.

<ItemGroup>
    <Compile Update=".\Services\CatalogService.*.cs">
        <DependentUpon>.\Services\CatalogService.cs</DependentUpon>
    </Compile>
</ItemGroup>  

Though, the question still remains...


Solution

  • Based on the documentation mentioned by Jimmy in comment, I tested in my VS2022 and it worked well.Firstly, nesting partial classes work for both Microsoft.NET.Sdk.Web and the rest of the projects (Microsoft.NET.Sdk) .You should check File nesting options in solution explorer.

    enter image description here

    Here’re several methods to nest files.

    1 when you select Default, if no settings exist for a given project type, then no files in the project are nested. You should create project-specific settings through the right-click menu (context menu) of the project console App (Microsoft.NET.Sdk).And use pathSegment to set the rules:  For example

    "pathSegment": {
            "add": {
              ".*": [
                ".cs",
              ]
            }
          }
    
    

    RESULT

    enter image description here

    pathSegment: Use this type of rule to nest Catelog.Paged.cs under Catelog.cs

    2 when you select Web: This option applies the Web file nesting behavior to all the projects in the current solution.

    3 customize file nesting for a solution. Select Add Custom Setting in solution explorer. You can add as many custom file nesting settings as you like. It’s similar to method 1.