Creating custom attributes

You can define your own attributes by inheriting from the Attribute class.

Add a class named CoderAttribute, as shown in the following code:

using System;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,                AllowMultiple = true)]public class CoderAttribute : Attribute{   public string Coder { get; set; }   public DateTime LastModified { get; set; }   public CoderAttribute(string coder, string lastModified)   {      Coder = coder;      LastModified = DateTime.Parse(lastModified);   }}

In Program, add a method named DoStuff, and decorate it with the Coder attribute, as shown in the following code:

[Coder("Mark Price", "22 August 2017")][Coder("Johnni Rasmussen", "13 September 2017")]public static void DoStuff(){}

In Program.cs ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition 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.