8.7. Implementing Our Own Attribute Class

An attribute is actually an instance of a class. We can introduce our own attributes, provided that the class representing an attribute inherits either directly or indirectly from the System.Attribute class. In addition, for nonabstract classes there must be at least one public constructor, and the class must have public accessibility. Here is a simple attribute class suggested from the Microsoft documentation. It defines an Author attribute that allows the programmer to tag her code with metadata attributes such as the implementor's name, the code version, and comment:

 [AttributeUsage( AttributeTargets.ClassMembers, AllowMultiple = true )] public class AuthorAttribute: Attribute { public AuthorAttribute( ...

Get C# Primer: A Practical 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.