前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CA1813:避免使用非密封特性

CA1813:避免使用非密封特性

作者头像
呆呆
发布2022-02-19 18:56:00
3050
发布2022-02-19 18:56:00
举报
文章被收录于专栏:centosDaicentosDai

规则 ID

CA1813

类别

“性能”

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

重大

原因

继承自 System.Attribute 的公共类型不是抽象类型,也不会密封(Visual Basic 中的 NotInheritable)。

规则说明

.NET 提供用于检索自定义特性的方法。 默认情况下,这些方法搜索特性继承层次结构。 例如,System.Attribute.GetCustomAttribute 搜索指定的特性类型或扩展指定特性类型的所有特性类型。 密封特性后,无需通过继承层次结构进行搜索,且能够提高性能。

如何解决冲突

若要解决此规则的冲突,请密封特性类型或使其成为抽象类型。

何时禁止显示警告

可安全地禁止显示此规则的警告。 仅当你正在定义特性层次结构,并且不能密封特性或使其成为抽象特性时才禁止显示。

示例

下面的示例显示了一个符合此规则的自定义特性。

// Satisfies rule: AvoidUnsealedAttributes.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]

public sealed class DeveloperAttribute : Attribute

{

private string nameValue;

public DeveloperAttribute(string name)

{

nameValue = name;

}

public string Name

{

get

{

return nameValue;

}

}

}

Imports System

Namespace ca1813

' Satisfies rule: AvoidUnsealedAttributes.

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct)>

Public NotInheritable Class DeveloperAttribute

Inherits Attribute

Public Sub New(name As String)

Me.Name = name

End Sub

Public ReadOnly Property Name() As String

End Class

End Namespace

相关规则

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

CA1018:用 AttributeUsageAttribute 标记特性

请参阅

特性

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

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

本文系外文翻译前往查看

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

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