我试图在VisualStudio2022中的C#文件中设置.editorconfig样式规则,因此它将始终生成我在项目中使用的代码样式。
目前,VS使用String和Int32而不是string和int生成属性或字段。尽管我知道string是String的别名,但在定义类型时,我希望使用string和int。
但是,在尝试访问静态方法(如String.IsNullOrEmpty(字符串) )时,我希望使用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文件中添加哪些规则来允许VisualStudio2022遵循上述样式?
发布于 2022-07-24 20:37:40
从找到的文档中找到这里
当将dotnet_style_predefined_type_for_locals_parameters_members设置为true时,VS将更倾向于局部变量、方法参数和类成员的language关键字。
当将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 of error在项目中强制执行代码样式。warning将在不兼容类型下显示一条卷曲行,而error则强制编译器失败。
下面是如何使用warning强制执行样式的示例
dotnet_style_predefined_type_for_locals_parameters_members = true : warning
dotnet_style_predefined_type_for_member_access = false : warninghttps://stackoverflow.com/questions/73094071
复制相似问题