繁体   English   中英

如何摆脱 Visual Studio 中的命名规则违规消息?

[英]How to get rid of Naming rule violation messages in Visual Studio?

我刚刚安装了 Visual Studio 2017。当我打开现有网站时,我收到各种警告消息,例如:

IDE1006 命名规则违规:这些单词必须以大写字符开头:swe_calc

在代码中它被定义为:

[System.Runtime.InteropServices.DllImport("swedll32.dll")]
public static extern Int32 swe_calc(double tjd, int ipl, Int32 iflag, IntPtr xx, IntPtr serr);

这也发生在我的 ASP.Net 控件中。 以 DropDownList 为例:

IDE1006 命名规则违规:这些单词必须以大写字符开头:ddlMonth_SelectedIndexChanged

如何在 Visual Studio 下消除这些类型的警告?

它是一个新的可配置功能,如果你去

工具 → 选项 → 文本编辑器 → 您的语言(我做过 C#) → 代码样式 → 命名

在那里我去管理样式添加骆驼案例(它在那里,但你需要将它添加到你的选择中):转到“+”号,然后相应地添加你的规则。

重要提示:关闭您的解决方案并重新打开它以使更改生效。

例如,我只对私有方法使用驼峰式大小写。 所以我选择了私有方法并要求为我创建的新方法“骆驼案例”设置样式并将其设置为严重性建议(我也将其提升到顶部)。

内置的也是所有“建议”,因此您也可以关闭消息。

如果只想在某些文件或区域中抑制它,可以使用以下命令:

#pragma warning disable IDE1006

// the code with the warning

#pragma warning restore IDE1006

如果您需要摆脱这些消息,您也可以将它们隐藏。

在此处输入图片说明

您可以重命名该方法并将名称添加到具有EntryPoint属性的属性。

[System.Runtime.InteropServices.DllImport("swedll32.dll", EntryPoint = "swe_calc")]
public static extern Int32 SweCalc(double tjd, int ipl, Int32 iflag, IntPtr xx, IntPtr serr);

如果要在方法中省略或取消警告消息,可以使用命名空间System.Diagnostics.CodeAnalysis 中SuppressMessage

[SuppressMessage("Microsoft.Design", "IDE1006", Justification = "Rule violation aceppted due blah blah..")]

Justification属性是可选的,但值得花一点时间写一个理由,让您的团队知道代码已修改并且没问题。

如果将鼠标悬停在命名规则违规上,则可以使用 Alt + Enter 调出该语言的命名样式。 您也可以使用工具 -> 选项 -> 文本编辑器 -> {语言} -> 代码样式 -> 命名。

对于方法上的驼峰式规则,您可以添加新规则并将其设置为驼峰式。 当您关闭代码文件并再次打开它时,您不应再看到该警告。 不知道为什么这不是默认选项,但在我的情况下不是(使用 Visual Code 15.8)。 我必须编辑样式以符合我们公司的标准。

示例 C# 命名样式设置

这可以使用.editorconfig设置文件使用普通 VS2017 和 VS2019 来完成,使用命名规则: https : .editorconfig

该文件可以手动创建,或者在 VS2019 中,您可以让 Visual Studio 从您的首选项中创建它(即在按照https://stackoverflow.com/a/41131563/131701配置您的首选项之后),点击从设置按钮生成编辑器配置文件。

从设置按钮生成编辑器配置文件

例如,以下规则集将为所有非公共方法启用驼峰命名,并保留 VS 附带的其他默认命名规则。

#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.private_method_should_be_camelcasestyle.severity = suggestion
dotnet_naming_rule.private_method_should_be_camelcasestyle.symbols = private_method
dotnet_naming_rule.private_method_should_be_camelcasestyle.style = camelcasestyle

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.interface.required_modifiers = 

dotnet_naming_symbols.private_method.applicable_kinds = method
dotnet_naming_symbols.private_method.applicable_accessibilities = private, protected, internal, protected_internal
dotnet_naming_symbols.private_method.required_modifiers = 

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.types.required_modifiers = 

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.non_field_members.required_modifiers = 

# Naming styles

dotnet_naming_style.pascal_case.required_prefix = 
dotnet_naming_style.pascal_case.required_suffix = 
dotnet_naming_style.pascal_case.word_separator = 
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix = 
dotnet_naming_style.begins_with_i.word_separator = 
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.camelcasestyle.required_prefix = 
dotnet_naming_style.camelcasestyle.required_suffix = 
dotnet_naming_style.camelcasestyle.word_separator = 
dotnet_naming_style.camelcasestyle.capitalization = camel_case

禁用规则。 右键单击错误消息并选择严重性为无

该规则断言字段必须是私有的。

您可以通过在字段后添加 {get;set;} 将其转换为属性

这为我消除了错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM