15.8. Building Custom Attributes

The first step in building a custom attribute is to create a new class deriving from System.Attribute. Keeping in step with the automobile theme used throughout this book, assume you have created a brand new C# Class Library project named AttributedCarLibrary. This assembly will define a handful of vehicles, each of which is described using a custom attribute named VehicleDescriptionAttribute:

// A custom attribute.
public sealed class VehicleDescriptionAttribute : System.Attribute
{
  public string Description { get; set; }

  public VehicleDescriptionAttribute(string vehicalDescription)
  {
    Description = vehicalDescription;
}
  public VehicleDescriptionAttribute(){ }
}

As you can see, VehicleDescriptionAttribute maintains ...

Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.