我正在使用 C# 10 的新特性 文件作用域命名空间声明
。
我有类似这样的旧代码
namespace SampleCode
{
public class MyClass
{
}
}
我正在将这段代码移至
namespace SampleCode;
public class MyClass
{
}
但我有很多警告:IDE0160: 转换为块作用域命名空间
我该如何确保人们仅使用旧语法时才会收到警告?
我正在使用 C# 10 的新特性 文件作用域命名空间声明
。
我有类似这样的旧代码
namespace SampleCode
{
public class MyClass
{
}
}
我正在将这段代码移至
namespace SampleCode;
public class MyClass
{
}
但我有很多警告:IDE0160: 转换为块作用域命名空间
我该如何确保人们仅使用旧语法时才会收到警告?
要在编辑器配置中控制代码风格,请使用以下行:
强制执行此样式
namespace SampleCode
{
public class MyClass
{
}
}
在 .editorconfig
文件中添加这一行。# IDE0160: Convert to block-scoped namespace
csharp_style_namespace_declarations = block_scoped:warning
为了执行这种风格
namespace SampleCode;
public class MyClass
{
}
在.editorconfig
中添加此行
# IDE0160: Convert to file-scoped namespace
csharp_style_namespace_declarations = file_scoped:warning
JetBrains Rider从版本2021.3.2开始支持dotnet_diagnostic.IDE*语法。
这简化了所有可能场景的设置:
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
这将涵盖所有情况。以下是原始答案。仍然值得阅读。
根据您的期望状态、使用的 IDE 和工作流程,有几个不同的设置需要控制。
它们在this文章中描述,我强烈建议您在开始为项目构建.editorconfig
之前阅读此文。
以下是文件范围和块范围 usings 的 EditorConfig/CSproj 设置摘要。
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
option = rule:severity
的语法迟早会被弃用。
csharp_style_namespace_declarations = file_scoped:error
Rider不支持dotnet_diagnostic.IDE*语法。
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
当前的.NET EditorConfig语法是否混乱?绝对是的。
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
option = rule:severity
的语法迟早会被弃用。
csharp_style_namespace_declarations = block_scoped:error
Rider不支持dotnet_diagnostic.IDE*语法。
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
IDE0160
和 IDE0161
- 它们有什么区别? - lonix