CA1018: Mark attributes with AttributeUsageAttribute

TypeName

MarkAttributesWithAttributeUsage

CheckId

CA1018

Category

Microsoft.Design

Breaking Change

Breaking

Cause

The System.AttributeUsageAttribute attribute is not present on the custom attribute.

Rule Description

When you define a custom attribute, mark it by using AttributeUsageAttribute to indicate where in the source code the custom attribute can be applied. The meaning and intended usage of an attribute will determine its valid locations in code. For example, you might define an attribute that identifies the person who is responsible for maintaining and enhancing each type in a library, and that responsibility is always assigned at the type level. In this case, compilers should enable the attribute on classes, enumerations, and interfaces, but should not enable it on methods, events, or properties. Organizational policies and procedures would dictate whether the attribute should be enabled on assemblies.

The System.AttributeTargets enumeration defines the targets that you can specify for a custom attribute. If you omit AttributeUsageAttribute, your custom attribute will be valid for all targets, as defined by the All value of AttributeTargets enumeration.

How to Fix Violations

To fix a violation of this rule, specify targets for the attribute by using AttributeUsageAttribute. See the following example.

When to Suppress Warnings

You should fix a violation of this rule instead of excluding the message. Even if the attribute inherits AttributeUsageAttribute, the attribute should be present to simplify code maintenance.

Example

The following example defines two attributes. BadCodeMaintainerAttribute incorrectly omits the AttributeUsageAttribute statement, and GoodCodeMaintainerAttribute correctly implements the attribute that is described earlier in this section. Note that the property DeveloperName is required by the design rule CA1019: Define accessors for attribute arguments and is included for completeness.

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;
         }
      }
   }
}

CA1019: Define accessors for attribute arguments

CA1813: Avoid unsealed attributes

See Also

Other Resources

Attribute Usage Guidelines