3.14. Inspect the Attributes of a Program Element Using Reflection

Problem

You need to use reflection to inspect the custom attributes applied to a program element.

Solution

All program elements, such as classes and subroutines, implement the System.Reflection. ICustomAttributeProvider interface. Call the IsDefined method of the ICustomAttributeProvider interface to determine whether an attribute is applied to a program element, or call the GetCustomAttributes method of the ICustomAttributeProvider interface to obtain objects representing the attributes applied to the program element.

How It Works

All the classes that represent program elements implement the ICustomAttributeProvider interface. This includes Assembly, Module, Type, EventInfo, ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach 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.