前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CA1018:用 AttributeUsageAttribute 标记特性

CA1018:用 AttributeUsageAttribute 标记特性

作者头像
用户4268038
发布2022-01-10 09:25:44
1710
发布2022-01-10 09:25:44
举报
文章被收录于专栏:stcnb

“值”

RuleId

CA1018

类别

设计

修复是中断修复还是非中断修复

重大

原因

自定义特性上不存在 System.AttributeUsageAttribute 特性。

规则说明

当定义自定义特性时,用 AttributeUsageAttribute 标记该特性,以指示源代码中可以应用自定义特性的位置。 特性的含义和预定用法将决定它在代码中的有效位置。 例如,你可以定义一个特性,该特性标识负责维护和增强库中的每个类型的人员,并且此责任始终在类型级别上分配。 在这种情况下,编译器应在类、枚举和接口上启用该特性,但不应在方法、事件或属性上启用它。 组织策略和过程将规定是否应在程序集上启用该特性。

System.AttributeTargets 枚举定义可为自定义特性指定的目标。 如果省略 AttributeUsageAttribute,则自定义特性将对所有目标有效,如 AttributeTargets 枚举的 All 值所定义。

如何解决冲突

若要解决此规则的冲突,请使用 AttributeUsageAttribute 指定特性的目标。 请参阅以下示例。

何时禁止显示警告

应解决此规则的冲突,而不是排除消息。 即使该特性继承 AttributeUsageAttribute,也应该提供该特性以简化代码维护。

示例

下面的示例定义了两个特性。 BadCodeMaintainerAttribute 错误地省略了 AttributeUsageAttribute 语句,但 GoodCodeMaintainerAttribute 正确实现了本部分前面所述的特性。 (设计规则 CA1019:定义特性参数的访问器要求属性 DeveloperName,出于完整性考虑,此属性包含在内。)

using System;

namespace ca1018

{

// Violates rule: MarkAttributesWithAttributeUsage.

public sealed class BadCodeMaintainerAttribute : Attribute

{

public BadCodeMaintainerAttribute(string developerName)

{

DeveloperName = developerName;

}

public string DeveloperName { get; }

}

// Satisfies rule: Attributes specify AttributeUsage.

// This attribute is valid for type-level targets.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]

public sealed class GoodCodeMaintainerAttribute : Attribute

{

public GoodCodeMaintainerAttribute(string developerName)

{

DeveloperName = developerName;

}

public string DeveloperName { get; }

}

}

Imports System

Namespace ca1018

' Violates rule: MarkAttributesWithAttributeUsage.

Public NotInheritable Class BadCodeMaintainerAttribute

Inherits Attribute

Public Sub New(developerName As String)

Me.DeveloperName = developerName

End Sub 'New

Public ReadOnly Property DeveloperName() As String

End Class

' Satisfies rule: Attributes specify AttributeUsage.

' The attribute is valid for type-level targets.

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Enum Or

AttributeTargets.Interface Or AttributeTargets.Delegate)>

Public NotInheritable Class GoodCodeMaintainerAttribute

Inherits Attribute

Public Sub New(developerName As String)

Me.DeveloperName = developerName

End Sub 'New

Public ReadOnly Property DeveloperName() As String

End Class

End Namespace

相关规则

CA1019:定义特性参数的访问器

CA1813:避免使用非密封特性

请参阅

特性

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档