使用“nameof”(IDE0280)

财产 价值
规则 ID IDE0280
标题 使用“nameof”
类别 样式
子类别 语言规则(参数首选项)
适用的语言 C#

概述

此规则标记在采用参数名称的属性(如 NotNullIfNotNullAttributeNotNullWhenAttributeCallerArgumentExpressionAttribute)中使用文本参数名称,而不是 nameof 表达式

选项

此规则没有关联的代码样式选项。

// Code with violations.
class C
{
    void M([NotNullIfNotNull("input")] string? input) { }
}

// Fixed code.
class C
{
    void M([NotNullIfNotNull(nameof(input))] string? input) { }
}

禁止显示警告

如果只想取消单个冲突,请将预处理器指令添加到源文件以禁用,然后重新启用规则。

#pragma warning disable IDE0280
// The code that's violating the rule is on this line.
#pragma warning restore IDE0280

若要禁用文件、文件夹或项目的规则,请将其严重性设置为 配置文件中的 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0280.severity = none

若要禁用所有代码样式规则,请将类别 Style 的严重性设置为 配置文件中的 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

有关详细信息,请参阅 如何取消代码分析警告