如何强制visualstudio使用小写类型(例如,字符串而不是字符串)生成属性



我正试图在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遵循上述样式?

从这里找到的文档

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

最新更新