// Example 16.26 (Figure 16.6) using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class)] // Documentation attribute can applied only to classes public class DocumentationAttribute : System.Attribute { public string author; public string date; // these should perhaps be properties public double revision; public string docString; public DocumentationAttribute(string a, string d, double r, string s) { author = a; date = d; revision = r; docString = s; } } [Documentation("Michael Scott", "July, 2015", 0.1, "Illustrates the use of attributes")] public class Attr { public static void Main(string[] args) { System.Reflection.MemberInfo tp = typeof(Attr); object[] attrs = tp.GetCustomAttributes(typeof(DocumentationAttribute), false); // false means don't search ancestor classes and interfaces DocumentationAttribute a = (DocumentationAttribute) attrs[0]; Console.WriteLine("author: " + a.author); Console.WriteLine("date: " + a.date); Console.WriteLine("revision: " + a.revision); Console.WriteLine("docString: " + a.docString); } }