Reflecting Attributes

Attributes are about application’s metadata. Because of this, you can use Reflection (see Chapter 47, “Reflection,” for details) for checking if a type recurs to custom attributes and investigate metadata (that is, application information). To accomplish this you invoke the System.Reflection.MemberInfo.GetCustomAttributes and System.Reflection.Attributes.GetCustomAttributes shared methods. The first one returns all attributes applied to the specified type whereas the second one returns an array of custom attributes applied to an assembly, a type or its members, and method parameters. The following is the most basic example for retrieving information about attributes applied to members of the Document class:

The following ...

Get Visual Basic® 2010 Unleashed 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.