Create, Apply, and Identify a Custom Attribute

Problem

You want to use custom attributes to decorate members and classes.

Solution

Create a class that derives from System.Attribute, apply it to a class or a member, and use the Type.GetCustomAttributes method to retrieve it during reflection.

Discussion

Attributes are a cornerstone of .NET extensibility. Using attributes, you can specify additional information about a type or a member that doesn’t relate directly to the code. For example, .NET uses attributes to tell the debugger how to treat code, to tell Microsoft Visual Studio .NET how to display components and controls in the Properties windows, to implement COM+ services such as object pooling, and to support Web Services and Web Service–related ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook 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.