我正在尝试使用 dotnet format 命令转换大型 C# 解决方案以利用文件范围的命名空间。
我尝试在解决方案的 .editorconfig 中注释我对它们的偏好
# from https://www.meziantou.net/updating-your-project-to-use-file-scoped-namespaces.htm
csharp_style_namespace_declarations=file_scoped:suggestion
和
# from https://stackoverflow.com/questions/71652548/rider-editorconfig-file-scoped-namespaces-not-respected
csharp_namespace_body = file_scoped
但是,当我运行dotnet format My.sln
时,它会进行一些空格格式修复(例如删除尾随空格),但不会转换为文件范围的命名空间。这应该有效吗?我错过了什么?
我使用的是 6.0.201dotnet
,我还尝试下载dotnet-format
的更新版本(7.0.321101+ec7537dd0645b35fc9057006639ca41e37d16348)。
解决此问题的一种方法是将该suggestion
增加到error
或warning
。
csharp_style_namespace_declarations = file_scoped:error
然后它将被dotnet format
拾起,就像你已经在做的那样。
dotnet format My.sln
<小时 />或者,您可以将其留给suggestion
csharp_style_namespace_declarations=file_scoped:suggestion
并将--severity info
传递给dotnet format
dotnet format My.sln --severity info
默认情况下,dotnet format
以--severity warn
运行,这会应用配置warning
和error
严重性的.editorconfig
设置,但不会应用具有suggestion
的设置。
来自有关--severity
的文档
要修复的诊断的最低严重性。允许的值为
info
、warn
和error
。默认值为warn
。