Home > Software design >  How do you nest/merge 2 or more partial classes .net vs2022
How do you nest/merge 2 or more partial classes .net vs2022

Time:10-05

I have a set of partial classes and I would like to be nested.

If I wanted to do it manually what do I need to modify? There were some visual studio extensions in the past but not for vs2022

thanks

Update

Suppose I have a class called MyService.cs but I would like to create partial classes like

MyService.AAA.cs MyService.BBB.cs MyService.CCC.cs

I would like them to be nested like pic below (just a sample)

enter image description here

CodePudding user response:

Add the following to your .csproj file:

<ItemGroup>
  <ProjectCapability Include="ConfigurableFileNesting" />
  <ProjectCapability Include="ConfigurableFileNestingFeatureEnabled" />
</ItemGroup>

And then if you look in Solution Explorer there should be a button you can click to turn the nesting on and off:

Button to enable file nesting

(It's the button just above the top-left of the tooltip in the image.)

You may need to close and reopen the project for the change to the .csproj file to take effect.

NOTE: This applies to SDK-style .csproj files only.

Also note that if you're using a web app, there's a separate setting which you select by clicking the drop-arrow on the right of the File Nesting button:

Web settings

  • Related