如何在 ASP.Net Core 中使用文件嵌套来嵌套接口

Mat*_*Mut 6 visual-studio visual-studio-2019

我想在 Visual Studio 中使用 Microsoft 的自定义文件嵌套选项来嵌套实现类文件及其“父”接口,因此在本示例中,我希望在解决方案资源管理器中ReportRepository.cs嵌套出现IReportRepository.cs。所有存储库都应该如此。

解决方案资源管理器中的文件

我已经在.filenesting.json解决方案的根目录中创建了该文件,但我不知道如何使用它并且文档非常稀疏

有谁知道我如何在 Visual Studio 2019 中实现这一目标?

ble*_*aky 7

据我所知,不可能按前缀分组。您只能按扩展名或后缀或整个文件名进行分组。

这意味着您无法为此定义规则。但是有2个选择:

  • 为每个文件定义规则
  • 更改命名方案

为每个文件定义规则

要为每个文件定义规则,您将使用fileToFile提供程序。这看起来像这样:

{
  "help": "https://go.microsoft.com/fwlink/?linkid=866610",
  "root": true,

  "dependentFileProviders": {
    "add": {
      "fileToFile": {
        "add": {
          "IInvestigationRepository.cs": [ // Parent file
            "InvestigationRepository.cs"   // Nested file
          ],
          "IReporterRepository.cs": [ // Parent file
            "ReporterRepository.cs"   // Nested file
          ]
          ...
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用不同的命名方案

或者,如果第一个选项不符合您的喜好,您可以为文件使用不同的命名方案,并让类保留其旧名称。

然后你可以fileSuffixToExtension像这样使用提供者:

{
  "help": "https://go.microsoft.com/fwlink/?linkid=866610",
  "root": true,

  "dependentFileProviders": {
    "add": {
      "pathSegment": {
        "add": {
          ".DefaultImplementation.cs": [
            ".cs"
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这将映射

  • IInvestigationRepository.DefaultImplementation.csIInvestigationRepository.cs
  • IReporterRepository.DefaultImplementation.csIReporterRepository.cs

等等。

这也将使您的文件的目的更清晰,因为仅具有相同的名称并不能告诉您它对接口做了什么,只是与接口有关。

如果您不想使用符号 with.并且更喜欢使用-,则可以通过替换"pathSegment""fileSuffixToExtension",并替换".DefaultImplementation.cs""-DefaultImplementation.cs"

  • 其他人请注意:这不适用于 .NET Standard 2.0 项目。请参阅 Microsoft 文档顶部有关此功能的重要说明:https://learn.microsoft.com/en-us/visualstudio/ide/file-nesting-solution-explorer?view=vs-2019 “该功能目前仅支持 ASP.NET Core 项目。” 正在尝试各种方法让它在 .NET Standard 中工作...然后终于阅读了顶部部分... (2认同)

小智 6

我们在 .csproj 文件中使用以下模式来自动嵌套混凝土及其接口(如果它们位于同一目录中)。

<ItemGroup>
    <Compile Update="**\*.cs">
        <DependentUpon>$([System.String]::Copy(I%(Filename).cs))</DependentUpon>
    </Compile>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

我知道问题是使用 Visual Studio 中的自定义嵌套选项;然而,在微软添加前缀功能之前,这种方法就可以发挥作用。