【问题标题】:What's the difference between dotnet_diagnostics and equivalent rules under dotnet_style_, csharp_ etc.?dotnet_diagnostics 与 dotnet_style_、csharp_ 等下的等效规则有什么区别?
【发布时间】:2022-08-07 21:23:13
【问题描述】:
例如,在.editorconfig 中,我可以要求字段为只读,同时使用:
# IDE0044: Make field readonly
dotnet_diagnostic.IDE0044.severity = warning
和
dotnet_style_readonly_field = true:warning
两者都将显示在编辑器中,两者都会导致问题在运行dotnet format 时得到修复。两种选择有什么区别?
标签:
visual-studio
roslyn
editorconfig
【解决方案1】:
编译器不知道 option = value:severity 语法,例如:
dotnet_style_readonly_field = true:warning
因此,如果您使用它,构建只会为 IDE 实时分析产生警告/错误。但是,如果您想在构建时强制执行代码样式,则需要使用 dotnet_diagnostic.RuleId.severity = severity 语法。
【解决方案2】:
The previous answer 并不完全正确,因为它在 .editorconfig 之外缺少 EnforceCodeStyleInBuild = true 设置,但是答案复杂得多,请参阅this 文章。
在实践中,你必须考虑你想要执行什么规则,你有什么 IDE 和这些 IDE 的版本,如果你有 CI/CD 等等。