EditorConfig 控制文件范围的命名空间声明

Swe*_*ell 34 c# editorconfig c#-10.0

我正在使用 C# 10 新功能File-scoped namespace declaration

我有这样的旧代码

namespace SampleCode
{
    public class MyClass
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在将此代码移至

namespace SampleCode;

public class MyClass
{
}
Run Code Online (Sandbox Code Playgroud)

但我有很多警告:IDE0160: Convert to block scoped namespace

我如何确保人们只会收到旧语法的警告?

Swe*_*ell 37

要控制 editorconfig 中的代码样式,请使用以下行:

为了强制执行这种风格

namespace SampleCode
{
    public class MyClass
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

添加这一行.editorconfig

# IDE0160: Convert to block-scoped namespace
csharp_style_namespace_declarations = block_scoped:warning
Run Code Online (Sandbox Code Playgroud)

为了强制执行这种风格

# IDE0160: Convert to block-scoped namespace
csharp_style_namespace_declarations = block_scoped:warning
Run Code Online (Sandbox Code Playgroud)

添加这一行.editorconfig

# IDE0160: Convert to file-scoped namespace
csharp_style_namespace_declarations = file_scoped:warning
Run Code Online (Sandbox Code Playgroud)

  • 在 .editorconfig 中强制执行它可以选择将整个解决方案重构为 VS2022 预览版中可用的文件范围命名空间,太棒了! (8认同)

KUT*_*ime 19

更新2022-01-27(所有场景设置)

从版本 2021.3.2 开始,JetBrains Rider 支持 dotnet_diagnostic.IDE* 语法。
这将所有可能场景的设置简化为:

编辑器配置

csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

CS项目

csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

这将涵盖所有场景。原答案如下。还是值得一读的。


您应该根据所需的状态、使用的 IDE 和工作流程来控制多种不同的设置。

本文对它们进行了描述,我强烈建议您在开始构建项目之前阅读该文章。.editorconfig

以下分别是文件范围和块范围使用的摘要。

用于文件范围使用的 EditorConfig/CSproj 设置


Visual Studio(违规错误)

编辑器配置

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

笔记

语法迟早option = rule:severity会被弃用。


JetBrains Rider(违规错误)

编辑器配置

csharp_style_namespace_declarations = file_scoped:error
Run Code Online (Sandbox Code Playgroud)

笔记

Rider 不支持 dotnet_diagnostic.IDE* 语法。


CLI 构建,例如 CI/CD 管道

编辑器配置

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

CS项目

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

推荐设置

编辑器配置

csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

CS项目

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

笔记

当前的 .NET EditorConfig 语法是否混乱?确实

用于块范围使用的 EditorConfig/CSproj 设置


Visual Studio(违规错误)

编辑器配置

csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)

笔记

语法迟早option = rule:severity会被弃用。


JetBrains Rider(违规错误)

编辑器配置

csharp_style_namespace_declarations = block_scoped:error
Run Code Online (Sandbox Code Playgroud)

笔记

Rider 不支持 dotnet_diagnostic.IDE* 语法。


CLI 构建,例如 CI/CD 管道

编辑器配置

csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)

CS项目

csharp_style_namespace_declarations = file_scoped:error
Run Code Online (Sandbox Code Playgroud)

推荐设置

编辑器配置

csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)

CS项目

csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)

  • @lonix IDE0160 = 块作用域,IDE0161 = 文件作用域 (5认同)