如何配置VS2022的.editorconfig实现指定C#类型命名风格
在Visual Studio 2022中通过.editorconfig配置C#类型风格规则
你需要在项目的.editorconfig文件中添加以下两条规则,就能实现定义类型用别名、调用静态成员用系统类型原名的要求:
# 字段、属性、参数、局部变量的类型声明使用预定义别名(string、int等) dotnet_style_predefined_type_for_locals_parameters_members = true:warning # 访问静态成员时使用系统类型原名(String、Int32等),而非别名 dotnet_style_predefined_type_for_member_access = false:warning
规则说明:
dotnet_style_predefined_type_for_locals_parameters_members = true:warning:强制在声明字段、属性、方法参数、局部变量时使用C#预定义的类型别名(如string、int),替代对应的.NET系统类型名称(String、Int32)。这会直接影响Visual Studio自动生成的属性、字段、参数代码。dotnet_style_predefined_type_for_member_access = false:warning:禁止使用别名访问系统类型的静态成员,要求使用原名(如String.IsNullOrEmpty而非string.IsNullOrEmpty),编辑器会自动修正写法并对违规代码显示警告。
添加完成后,Visual Studio 2022会自动应用这些规则,代码生成、格式化、代码提示都会完全匹配你想要的风格。
内容的提问来源于stack exchange,提问作者Jay
相关产品推荐
相关产品推荐

