Swe*_*ell 34 c# editorconfig c#-10.0
我正在使用 C# 10 新功能File-scoped namespace declaration。
我有这样的旧代码
namespace SampleCode
{
public class MyClass
{
}
}
Run Code Online (Sandbox Code Playgroud)
我正在将此代码移至
namespace SampleCode;
public class MyClass
{
}
Run Code Online (Sandbox Code Playgroud)
但我有很多警告:IDE0160: Convert to block scoped namespace
我如何确保人们只会收到旧语法的警告?
Swe*_*ell 37
要控制 editorconfig 中的代码样式,请使用以下行:
为了强制执行这种风格
namespace SampleCode
{
public class MyClass
{
}
}
Run Code Online (Sandbox Code Playgroud)
添加这一行.editorconfig
# IDE0160: Convert to block-scoped namespace
csharp_style_namespace_declarations = block_scoped:warning
Run Code Online (Sandbox Code Playgroud)
为了强制执行这种风格
# IDE0160: Convert to block-scoped namespace
csharp_style_namespace_declarations = block_scoped:warning
Run Code Online (Sandbox Code Playgroud)
添加这一行.editorconfig
# IDE0160: Convert to file-scoped namespace
csharp_style_namespace_declarations = file_scoped:warning
Run Code Online (Sandbox Code Playgroud)
KUT*_*ime 19
从版本 2021.3.2 开始,JetBrains Rider 支持 dotnet_diagnostic.IDE* 语法。
这将所有可能场景的设置简化为:
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
这将涵盖所有场景。原答案如下。还是值得一读的。
您应该根据所需的状态、使用的 IDE 和工作流程来控制多种不同的设置。
本文对它们进行了描述,我强烈建议您在开始构建项目之前阅读该文章。.editorconfig
以下分别是文件范围和块范围使用的摘要。
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
语法迟早option = rule:severity会被弃用。
csharp_style_namespace_declarations = file_scoped:error
Run Code Online (Sandbox Code Playgroud)
Rider 不支持 dotnet_diagnostic.IDE* 语法。
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)
当前的 .NET EditorConfig 语法是否混乱?确实。
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)
语法迟早option = rule:severity会被弃用。
csharp_style_namespace_declarations = block_scoped:error
Run Code Online (Sandbox Code Playgroud)
Rider 不支持 dotnet_diagnostic.IDE* 语法。
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = file_scoped:error
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error
Run Code Online (Sandbox Code Playgroud)
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Run Code Online (Sandbox Code Playgroud)