我正试图在Visual Studio 2022中的.editorconfig
文件中设置C#
样式规则,因此它将始终生成我在项目中使用的代码样式。
目前,VS使用String
和Int32
而不是string
和int
生成属性或字段。尽管我知道string
是String
的别名,但在定义类型时,我希望使用string
和int
。
但是,当尝试访问String.IsNullOrEmpty(String(等静态方法时,我希望使用String
而不是string
。所以样式看起来像这个
public class Test
{
// use lowercase here (string not String)
private string _name;
// use lowercase here (string not String)
public string Title { get; set; }
public Test(string name)
{
// use uppercase here (String.IsNullOrEmpty(name) not string.IsNullOrEmpty(name))
if (String.IsNullOrEmpty(name))
{
throw new Exception("...");
}
_name = name;
}
}
我可以向.editorconfig
文件添加哪些规则以允许Visual Studio 2022遵循上述样式?
从这里找到的文档
当将dotnet_style_predefined_type_for_locals_parameters_members
设置为true
时,VS将更喜欢局部变量、方法参数和类成员的语言关键字。
当将dotnet_style_predefined_type_for_member_access
设置为false
时,VS将更喜欢框架类。
简而言之,您需要将其添加到.editorconfig
文件中
dotnet_style_predefined_type_for_locals_parameters_members = true
dotnet_style_predefined_type_for_member_access = false
您可以选择在项目中使用warning
或error
来强制执行代码样式。warning
将在不兼容类型下显示一条卷曲的线,而error
则强制编译器失败。
以下是如何使用warning
强制样式的示例
dotnet_style_predefined_type_for_locals_parameters_members = true : warning
dotnet_style_predefined_type_for_member_access = false : warning