O'Reilly logo

Applied Microsoft® .NET Framework Programming by Jeffrey Richter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining Your Own Attribute

You know that an attribute is a type derived from System.Attribute, and you also know how to apply an attribute. Let’s now look at how to define your own custom attributes. Say that you’re the Microsoft employee responsible for adding the bit flag support to enumerated types. To accomplish this, the first thing you have to do is define a FlagsAttribute type:

namespace System {
   public class FlagsAttribute : System.Attribute {
      public FlagsAttribute() {
      }
   }
}

Notice that the FlagsAttribute type inherits from Attribute; this is what makes the FlagsAttribute type a CLS-compliant custom attribute. In addition, all nonabstract attributes must have public accessibility, and by convention all attribute type names should end with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required