Export (0) Print
Expand All
Expand Minimize

Define accessors for attribute arguments

TypeName

DefineAccessorsForAttributeArguments

CheckId

CA1019

Category

Microsoft.Design

Breaking Change

NonBreaking

In its constructor, an attribute defines arguments that do not have corresponding properties.

Attributes can define mandatory arguments that must be specified when applying the attribute to a target. These are sometimes called positional arguments because they are supplied to attribute constructors as positional parameters. For every mandatory argument, the attribute should also provide a corresponding read-only property so that the value of the argument can be retrieved at execution time. This rule checks to see that for each constructor parameter, you have defined the corresponding property.

Attributes can also define optional arguments, called named arguments. These arguments are supplied to attribute constructors by name and should have a corresponding read/write property.

For mandatory and optional arguments, the corresponding properties and constructor parameters should use the same name but different casing. Properties use Pascal casing, and parameters use camel casing.

To fix a violation of this rule, add a read-only property for each constructor parameter that does not have one.

Exclude a warning from this rule if you do not want the value of the mandatory argument to be retrievable.

The following example shows two attributes that define a mandatory (positional) parameter. The first implementation of the attribute is incorrectly defined. The second implementation is correct.

using System;

namespace DesignLibrary
{
// Violates rule: DefineAccessorsForAttributeArguments.

   [AttributeUsage(AttributeTargets.All)]
   public sealed class BadCustomAttribute :Attribute 
   {
      string data;

      // Missing the property that corresponds to 
      // the someStringData parameter.

      public BadCustomAttribute(string someStringData)
      {
         data = someStringData;
      }
   }

// Satisfies rule: Attributes should have accessors for all arguments.

   [AttributeUsage(AttributeTargets.All)]
   public sealed class GoodCustomAttribute :Attribute 
   {
      string data;

      public GoodCustomAttribute(string someStringData)
      {
         data = someStringData;
      }
      //The constructor parameter and property
      //name are the same except for case.

      public string SomeStringData
      {
         get 
         {
            return data;
         }
      }
   }
}

Positional and named arguments make it clear to consumers of your library which arguments are mandatory for the attribute and which arguments are optional.

The following example shows an implementation of an attribute with both positional and named arguments.

The following example illustrates how to apply the above custom attribute to two properties.

Community Additions

ADD
Show:
© 2014 Microsoft