Udostępnij za pośrednictwem


CA1018: Atrybutów znacznika z element AttributeUsageAttribute

TypeName

MarkAttributesWithAttributeUsage

CheckId

CA1018

Kategoria

Microsoft.Design

Zmiana kluczowa

Kluczowa

Przyczyna

Atrybut System.AttributeUsageAttribute nie jest obecny na niestandardowym atrybucie.

Opis reguły

Definiując atrybut niestandardowy, oznacz go za pomocą AttributeUsageAttribute, aby wskazać, gdzie w kodzie źródłowym atrybut niestandardowy może zostać zastosowany.Znaczenie i zamierzone użycie atrybutu określi jego prawidłowe lokalizacje w kodzie.Na przykład, można zdefiniować atrybut, który identyfikuje osobę odpowiedzialną za konserwację i ulepszanie każdego typu w bibliotece, ta odpowiedzialność jest zawsze przypisywana na poziomie typu.W takim przypadku kompilatory powinny włączyć ten atrybut w klasach, wyliczeniach i interfejsach, ale nie powinny włączać go w metodach, zdarzeniach i właściwościach.Zasady i procedury organizacyjne określają, czy atrybut powinien być włączony dla zestawów.

Enumeracja System.AttributeTargets określa cele, które można określić dla atrybutu niestandardowego.Jeżeli pominięto AttributeUsageAttribute, niestandardowy atrybut będzie ważny dla wszystkich celów, wyznaczony przez wartość All wyliczenia AttributeTargets.

Jak naprawić naruszenia

Aby naprawić naruszenie tej reguły, określ cele dla atrybutu używając AttributeUsageAttribute.Zobacz przykład poniżej.

Kiedy pominąć ostrzeżenia

Należy naprawić naruszenie tej reguły, zamiast wyłączyć wiadomość.Nawet jeśli atrybut dziedziczy z AttributeUsageAttribute, atrybut powinien być obecny, aby ułatwić konserwację kodu.

Przykład

W poniższym przykładzie zdefiniowano dwa atrybuty.BadCodeMaintainerAttribute niepoprawnie pomija instrukcję AttributeUsageAttribute, a GoodCodeMaintainerAttribute poprawnie implementuje atrybut, który został opisany w tej sekcji.Należy zauważyć, że właściwość DeveloperName jest wymagana przez regułę projektowania CA1019: Definiowanie akcesorów dla argumentów atrybutu. i jest uwzględniona dla kompletności.

Imports System

Namespace DesignLibrary

' Violates rule: MarkAttributesWithAttributeUsage.
NotInheritable Public Class BadCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
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)> _
NotInheritable Public Class GoodCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
End Class 

End Namespace
using System;

namespace DesignLibrary
{
// Violates rule: MarkAttributesWithAttributeUsage.

   public sealed class BadCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public BadCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
// Satisfies rule: Attributes specify AttributeUsage.

   // The attribute is valid for type-level targets.
   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
   public sealed class GoodCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public GoodCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
}

Powiązane reguły

CA1019: Definiowanie akcesorów dla argumentów atrybutu.

CA1813: Uniknąć niezamkniętych szczelnie atrybutów

Zobacz też

Informacje

Attribute Usage Guidelines