Creating Your Own Attributes

When the predefined system library attributes are not enough for development needs, it may be desirable to customize your own. This is similar to creating a normal class, except that it is decorated with the AttributeUsage attribute and inherit from the System.Attribute class.

The AttributeUsage Attribute

The AttributeUsage attribute specifies how an attribute can be used in a program. It tells what program elements the attribute can be used on, if the attribute can be used multiple times, and whether the attribute can be inherited.

Allowed Elements

Some attributes will only make sense on certain program elements. For example, the Flags attribute only makes sense on an enum and would be illogical if applied to anything ...

Get C# 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.