Create C# nested files in Visual Studio

Introduction

Microsoft Visual Studio supports nested files, which are usually appsettings.json. Other file types are supported by creating a file named .filenesting.json in the root of a C# project.

Example

In the Models folder, a Person class is a partial class separated into three files.

Shows from VS2022 Solution Explorer the the person files

To nest the three files created in the root folder of the project a file named nesting.json as shown below.

{
  "help": "https://go.microsoft.com/fwlink/?linkid=866610",
  "root": true,
  "dependentFileProviders": {
    "add": {
      "fileToFile": {
        "add": {
          "Person.Notification.cs": [
            "Person.cs"
          ],
          "Person.Sets.cs": [
            "Person.cs"
          ]
        }
      }
    }
  }
}

Note the organization for each node: a class file with the base file name, a descriptor, and an extension, followed by the base file name. The results are shown below.

From Solution Explorer shows the nested Person class

Using Copilot prompt files to create .filenesting.json

By creating a prompt file Copilot can create a nesting file.

For this example partial classes are targeted under the Models folder.

  • Under the root folder of the Visual Studio solution, add a file named NestedPartialClasses.prompt.md under the folder .githubprompts (see example in provided source code)

  • Add the following instructions

  1. If there is a Models folder find all partial classes in it.
  2. Collect each partial class files
  3. Create in the root folder of the current project a file named .filenesting.json
  4. In this file, create a JSON structure that nests all the partial class files under their main class file. The main class file will have a single period in the file name.
  • Open Copilot Chat window.
    • Select Agent mode
    • Select the project
    • Select the prompt file
    • Select press enter

Copilot reads the prompt file instructions and executes. Once finished, accept the new file if it is correct, which it should be. Examine that the class is now nested under Solution Explorer.

Chat window targeting the project PromptFilesExampleApp1.

Chat window

If the partial classes are in a different folder, adjust the instructions in the prompt file.

Source code

Source code

  • Prompt file: Solution root folder .githubpromptsNestedPartialClasses.prompt.md
  • Sample project: PromptFilesExamplesApp1

✔️ Note the following in the project file for permitting the use of the field keyword.

<LangVersion>preview</LangVersion>

Issue with multiple partial classes

The above fails to work so the solution is to use a Directory.Build.targets file at the root of the project.

File contents (its a rather long set of instructions).

What this does is create

objFileNesting.DependentUpon.generated.props

For testing, a second partial class, Customers, was added.

Next, for each base class the following was added above the class definition.

// NEST-ROOTs

objFileNesting.DependentUpon.generated.props contents for the sample project.

<Project>
  <ItemGroup>
    <Compile Update="ModelsCusomer.Other.cs">
      <DependentUpon>Customer.cs</DependentUpon>
    </Compile>
    <Compile Update="ModelsPerson.Notification.cs">
      <DependentUpon>Person.cs</DependentUpon>
    </Compile>
    <Compile Update="ModelsPerson.Sets.cs">
      <DependentUpon>Person.cs</DependentUpon>
    </Compile>
  </ItemGroup>
</Project>

Note
The code in Directory.Build.targets was written by ChatGPT which took an intial prompt resulting with 40 errors followed by feeding those errors back to ChatGPT until zero errors. The code worked but did not nest so I told ChatGPT to look into sub folders which finally nested the two classes.

Summary

The single file nesting is easy using .filenesting.json, but for multiple partial class files Directory.Build.targets is the answer. Also, the partial classes must reside in the same folder.

Leave a Reply