Reading Attribute Data

Learn how to create custom attributes to inject metadata into your code and use reflection to read and enforce that metadata at runtime.

We'll cover the following...

Attributes are special constructs that let us inject additional metadata into an assembly. Attributes can be applied both to the entire type and to individual parts of the type, such as properties, methods, and fields. Attributes have many applications, but we most often use them to validate that a member or type meets certain conditions.

Suppose we have a minimum length requirement for a password in our system. We have a class called UserProfile with the following definition: