Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

CA1018: Mark attributes with AttributeUsageAttribute

TypeName

MarkAttributesWithAttributeUsage

CheckId

CA1018

Category

Microsoft.Design

Breaking Change

Breaking

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

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.

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

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.

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.

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;
         }
      }
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.