【问题标题】:How to get rid of Naming rule violation messages in Visual Studio?如何摆脱 Visual Studio 中的命名规则违规消息?
【发布时间】:2017-04-12 21:02:36
【问题描述】:

我刚刚安装了 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 下消除这些类型的警告?

【问题讨论】:

  • 请务必将此反馈提交给 VS。 2017 是一个具有新功能的新版本,有时这些功能开始时可能过于激进。您的反馈将有助于调整默认行为。
  • 听起来像一个错误。这不仅与旧项目有关,还与在 VS2017rc 中创建的新项目有关。 VS 创建控件名称,然后将其标记为攻击性..
  • 我在使用 MSVS 生成的按钮单击处理程序“btnList_Click()”时遇到了这个 1006 错误。换句话说,MSVS2017 在抱怨 它自己创建的方法!它突然抱怨 自 .Net 1.0 以来一直存在的命名约定!叹息.. 理想的解决方案:#pragma warning disable IDE1006。这样做的好处是,因为它在源代码中,所以它会自动在项目范围内(您不必同步任何每个工作站的 MSVS 设置)。

标签: c# asp.net visual-studio-2017


【解决方案1】:

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

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

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

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

例如,我只将骆驼案例用于私有方法。所以我选择 Private Method 并要求 Style 我创建的新方法“camel Case”并将其设置为 Severity Suggestion(我也将其提升到顶部)。

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

【讨论】:

  • 这与我们用于私有方法的命名约定完全相同 - 谢谢!现在我只需要弄清楚如何为每个人启用此功能,而无需人们单独设置此选项....
  • 获得这些选项的快速方法是单击“快速操作”灯泡,将鼠标悬停在“修复名称违规”上,然后单击“更改样式选项”按钮。
  • 我刚刚删除了规则
  • 要与整个团队共享此类设置/规则/样式,请参阅:stackoverflow.com/questions/11684457/…
  • 整个命名功能在 Visual Studio 2019 版本中似乎存在错误且无法正常工作。
【解决方案2】:

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

#pragma warning disable IDE1006

// the code with the warning

#pragma warning restore IDE1006

【讨论】:

  • 这可能是“理想”的解决方案!它解决了整个团队的问题。
【解决方案3】:

如果您需要删除这些消息,您也可以直接隐藏它们。

【讨论】:

  • 在源代码中抑制它们与在抑制文件中抑制它们有什么区别?
  • @TylerH "In Source" 是引发警告的文件。 “In Suppression File”是一个生成的文件,用于存储您的所有警告。
【解决方案4】:

您可以重命名方法并将名称添加到具有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);

【讨论】:

  • 我也有一个下拉框,它给出了同样的错误:'ddlMonth_SelectedIndexChanged'。我所有的 asp.net 控件也必须重命名吗?
  • @SteveFerg 这取决于你——它只是一个警告。
  • 我想的也差不多。我只是想知道除了“取消单击”错误列表中的“消息”框之外,是否还有其他选项可以关闭它们。
  • 以后,extern 方法默认忽略此规则。 github.com/dotnet/roslyn/pull/51728
【解决方案5】:

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

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

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

【讨论】:

    【解决方案6】:

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

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

    Sample C# Naming Styles Settings

    【讨论】:

      【解决方案7】:

      这可以通过使用.editorconfig 设置文件的普通 VS2017 和 VS2019 来完成,使用命名规则:https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

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

      例如,下面这组规则将为所有非公共方法启用camelCase,并保留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
      

      【讨论】:

      • 我按照你的建议做了,没有得到结果!
      【解决方案8】:

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

      【讨论】:

      • 42 不再是答案。这就是答案!
      【解决方案9】:

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

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

      这为我消除了错误。

      【讨论】:

        猜你喜欢
        • 2015-09-03
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 2015-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多