使用 dotnet 格式切换到文件范围的命名空间



我正在尝试使用 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增加到errorwarning

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运行,这会应用配置warningerror严重性的.editorconfig设置,但不会应用具有suggestion的设置。

来自有关--severity的文档

要修复的诊断的最低严重性。允许的值为infowarnerror。默认值为warn

最新更新