如何强制Visual Studio使用小写类型生成属性(例如,使用string而不是String)

3

我正试图在 Visual Studio 2022 的 .editorconfig 文件中设置 C# 的样式规则,以便始终生成符合我项目中使用的代码风格。

目前,VS 生成属性或字段时使用的是 StringInt32,而不是 stringint。虽然我知道 stringString 的别名,但我想在定义类型时使用 stringint

然而,当尝试访问静态方法(例如 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遵循上述样式?
1个回答

3

此处找到的文档中:

当将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

可选的,您可以通过使用warningerror在项目中强制执行代码风格。当使用warning时,将在不符合规范的类型下显示一个卷曲线,而error则会导致编译器失败。

以下是使用warning强制执行样式的示例:

dotnet_style_predefined_type_for_locals_parameters_members = true : warning
dotnet_style_predefined_type_for_member_access = false : warning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接