3.13. Create a Custom Attribute

Problem

You need to create a custom attribute.

Solution

Create a class that derives from the abstract (MustInherit) base class System.Attribute. Implement constructors, fields, and properties to allow users to configure the attribute. Apply the System. AttributeUsageAttribute attribute to your class to define the following:

  • Which program elements are valid targets of the attribute

  • Whether you can apply more than one instance of the attribute to a program element

  • Whether the attribute is inherited by derived types

How It Works

Attributes provide a mechanism for associating declarative information (metadata) with program elements. This metadata is contained in the compiled assembly, allowing programs to retrieve it ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.