You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5代码样式分析器官方默认.editorconfig配置文件求助

.NET代码样式分析器官方默认.editorconfig配置方案

嘿,我来帮你搞定这个.NET代码样式分析器的默认配置问题!微软其实在Roslyn(.NET编译器平台)项目里提供了官方的代码样式默认配置,你既可以直接引用,也可以把完整配置复制到本地文件里实现精细控制,具体如下:

一、快速启用官方默认配置(推荐)

如果你不想手动复制一大段配置,只需在项目的.editorconfig里添加这一行,就能直接继承官方的默认规则:

# 启用.NET官方代码样式默认配置
inherit_from = https://raw.githubusercontent.com/dotnet/roslyn/main/.editorconfig

不过你提到想要更精细的本地控制,那下面给你贴出完整的官方默认配置内容,你可以直接复制到本地的.editorconfig文件中,再根据需求调整具体规则。

二、完整官方默认代码样式.editorconfig配置

# 标记为根配置,不会向上查找其他.editorconfig
root = true

[*.{cs,vb}]
# 基础格式:缩进、换行与制表符
indent_size = 4
indent_style = space
tab_width = 4
end_of_line = crlf

# 命名规则
# 接口必须以I开头,采用帕斯卡命名法
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = interface_naming_style

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected

dotnet_naming_style.interface_naming_style.required_prefix = I
dotnet_naming_style.interface_naming_style.capitalization = pascal_case

# 类、结构体、枚举、委托采用帕斯卡命名法
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case_style

dotnet_naming_symbols.types.applicable_kinds = class, struct, enum, delegate
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected

dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# 代码样式偏好规则
csharp_prefer_braces = true:silent
csharp_prefer_simple_default_expression = true:silent
csharp_style_pattern_matching_over_is_with_cast_check = true:silent
csharp_style_pattern_matching_over_as_with_null_check = true:silent
csharp_style_inlined_variable_declaration = true:silent
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent
csharp_prefer_null_check_over_type_check = true:silent
csharp_prefer_conditional_expression_over_assignment = true:silent
csharp_prefer_conditional_expression_over_return = true:silent

# 空行规则
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# 空格规则
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_parentheses = none
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_call_parameter_list_parentheses = none
csharp_space_between_method_declaration_parameter_list_parentheses = none
csharp_space_between_parentheses_in_control_flow_statements = none

三、自定义精细控制的小技巧

拿到这个默认配置后,你可以根据团队需求调整每个规则的severity级别:

  • suggestion:仅作为提示,不影响编译
  • warning:编译时发出警告
  • error:直接将规则设为编译错误

比如你想把“接口必须以I开头”的规则升级为警告,只需把对应的行改成:

dotnet_naming_rule.interface_should_be_begins_with_i.severity = warning

内容的提问来源于stack exchange,提问作者lonix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:57:48