【问题标题】:Why does Resharper think that these enums are never used?为什么 Resharper 认为这些枚举从未使用过?
【发布时间】:2013-01-30 19:54:19
【问题描述】:

我有这些枚举:

    private enum FontSizeType
    {
        XSmall, //9
        Small,  //12 
        Medium, //18
        Large,  //24
        XLarge, //36
        XXLarge //47
    }

    private enum AlignOptions
    {
        Left,
        Center,
        Right
    }

    private enum ValueType
    {
        Text,
        Barcode
    }

Resharper 的检查告诉我所有这些“枚举成员 'XSmall' [etc.] 从未使用过”

但我在组合框中使用它们,如下所示:

   comboBoxType1.DataSource = Enum.GetNames(typeof(ValueType));

...那么为什么 Resharper 会被愚弄呢?还是这样?

【问题讨论】:

  • Resharper 没有“看到”FontSizeType.XSmall (等...)的直接用法,因为您正在对 whole 枚举进行数据绑定。

标签: combobox enums resharper datasource automated-refactoring


【解决方案1】:

ReSharper 不会检测隐式用法。您可以使用 [UsedImplicitly] 告诉它您的类型成员被隐式使用,然后它应该停止抱怨。

为了在您的代码中使用 UsedImplicitlyAttribute,您应该包含对 JetBrains.Annotations.dll 的引用或在您的项目中包含一些复制粘贴的源代码,有关详细信息,请参阅 http://www.jetbrains.com/resharper/webhelp/Code_Analysis__Annotations_in_Source_Code.html

您应该在每个枚举值上添加 [UsedImplicitly]。

【讨论】:

  • 试试这个:[UsedImplicitly] private enum ValueType { Text, Barcode } ...我得到,“找不到类型或命名空间名称'UsedImplicitlyAttribute'(您是否缺少 using 指令或程序集参考?)”
  • 谢谢,但在这种情况下,我宁愿“忍受”Resharper 的指手画脚,而不是这样混淆我的来源。
【解决方案2】:

您也可以使用以下指令禁用投诉本身: [SuppressMessage("ReSharper", "UnusedMember.Global")] public enum ComplianceStatus { Notcompliant, Unknown, Warning, Compliant, Pendingrestart, Pendinglogoff }

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多