Creating Custom Attributes

Attributes are simply classes derived from the System.Attribute class. Attributes convey metadata—extra information—about entities in .NET. For this reason they are an essential part of .NET. Attributes can be subclassed just like any other class, thus permitting you to create custom attributes.

To create a custom attribute you need to create a new class that inherits from System.Attribute, apply the AttributeUsageAttribute to that class, and define named and positional arguments. I will demonstrate each of these techniques in the subsections that follow. (The sample solution for this section is the CustomAttributeDemo.sln file.)

The custom attribute constructed in this example facilitates the creation of a user interface ...

Get Visual Basic® .NET Power Coding 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.