2022-01-27 更新
Jetbrain Rider 从 2021.3.2 版本开始支持 dotnet_diagnostic.IDE* 语法。这简化了设置到
编辑器配置
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
这将涵盖所有场景。下面的原始答案。还是值得一读的。
您应该根据所需的状态、使用的 IDE 和工作流程来控制几种不同的设置。
它们在this 文章中有所描述,我强烈建议您在开始为您的项目构建.editorconfig 之前阅读这篇文章。
这里分别总结了文件范围和块范围的使用。
文件范围使用的EditorConfig/CSproj设置
Visual Studio(违规错误)
编辑器配置
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
注意
语法option = rule:severity 迟早会是deprecated。
JetBrain Rider(违规错误)
编辑器配置
csharp_style_namespace_declarations = file_scoped:error
注意
Rider 不支持 dotnet_diagnostic.IDE* 语法。
CLI 构建,例如 CI/CD 管道
编辑器配置
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
推荐设置
编辑器配置
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
注意
当前的 .NET EditorConfig 语法是不是一团糟? 肯定。
块范围使用的EditorConfig/CSproj设置
Visual Studio(违规错误)
编辑器配置
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
注意
语法option = rule:severity 迟早会是deprecated。
JetBrain Rider(违规错误)
编辑器配置
csharp_style_namespace_declarations = block_scoped:error
注意
Rider 不支持 dotnet_diagnostic.IDE* 语法。
CLI 构建,例如 CI/CD 管道
编辑器配置
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
推荐设置
编辑器配置
csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>