次の方法で共有


CA1813: シールされていない属性を使用しません

TypeName

AvoidUnsealedAttributes

CheckId

CA1813

[カテゴリ]

Microsoft.Performance

互換性に影響する変更点

あり

原因

System.Attribute から継承されたパブリック型が抽象型ではなく、シールされていません (Visual Basic では NotInheritable)。

規則の説明

.NET Framework クラス ライブラリには、カスタム属性を取得するメソッドが用意されています。既定で、このようなメソッドでは属性の継承階層が検索されます。たとえば、Attribute.GetCustomAttribute では、指定した属性型、または指定した属性型を拡張する属性型が検索されます。属性をシールすると、継承階層の全体が検索されなくなるため、パフォーマンスが向上します。

違反の修正方法

この規則違反を修正するには、属性型をシールするか、抽象型にします。

警告を抑制する状況

この規則による警告を抑制しても安全です。属性の階層構造を定義していて属性をシールできない場合、または抽象型にできない場合にのみ、除外します。

使用例

この規則に適合するカスタム属性を次の例に示します。

Imports System

Namespace PerformanceLibrary

' Satisfies rule: AvoidUnsealedAttributes.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct)>  _
NotInheritable Public Class DeveloperAttribute
    Inherits Attribute
    Private nameValue As String

    Public Sub New(name As String)
        nameValue = name
    End Sub


    Public ReadOnly Property Name() As String
        Get
            Return nameValue
        End Get
    End Property
End Class 

End Namespace
using System;

namespace PerformanceLibrary 
{
    // 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;
            }
        }
    }

}

関連規則

CA1019: 属性引数にアクセサーを定義します

CA1018: 属性を AttributeUsageAttribute に設定します

参照

関連項目

Attribute Usage Guidelines