You can also create your own custom attributes for storing metadata in a module or assembly (but not as new compiler directives). To create a new attribute, you use (surprise!) an attributethe AttributeUsage attribute: [AttributeUsage( validon, AllowMultiple=allowmultiple, Inherited=inherited )] Here are the parts of this attribute:
Here's an example of a custom attribute. In this case, we'll create an attribute named Author that will let us store data about the author of a section of code. We'll allow this attribute to be used on classes, constructors, fields, methods , and properties. Here's how to specify that using the [AttributeUsage] attribute: [AttributeUsage(AttributeTargets.Class AttributeTargets.Constructor AttributeTargets.Field AttributeTargets.Method AttributeTargets.Property, AllowMultiple = true)] We'll derive our new attribute, Author , from the System.Attribute class, giving it a constructor and properties to hold the code author's name , code creation time, and any comments the author wants to leave: [AttributeUsage(AttributeTargets.Class AttributeTargets.Constructor AttributeTargets.Field AttributeTargets.Method AttributeTargets.Property, AllowMultiple = true)] public class Author : System.Attribute { private string privateText; private string privateTime; private string privateName; public Author(string name, string time) { privateName = name; privateTime = time; } public string Text { get { return privateText; } set { privateText = value; } } public string Time { get { return privateTime; } } public string Name { get { return privateName; } } } Now we'll use this new attribute in code. You pass values to the attribute's constructor corresponding to the author's name and the time the code was authored . And you can also set the read/write Text property by assigning text to the property name Text , as you see in ch14_02.cs, Listing 14.2. Listing 14.2 Creating Custom Attributes (ch14_02.cs)using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class AttributeTargets.Constructor AttributeTargets.Field AttributeTargets.Method AttributeTargets.Property, AllowMultiple = true)] public class Author : System.Attribute { private string privateText; private string privateTime; private string privateName; public Author(string name, string time) { privateName = name; privateTime = time; } public string Text { get { return privateText; } set { privateText = value; } } public string Time { get { return privateTime; } } public string Name { get { return privateName; } } } [Author("Cary Grant", "11/25/48")] [Author("Grace Kelly", "11/25/48", Text="Hi Cary!")] public class Displayer { public void Display() { System.Console.WriteLine("No worries!"); } } public class ch14_02 { public static void Main() { Displayer displayer = new Displayer(); displayer.Display(); } } If you run ch14_02.cs, you won't see any sign of the new attribute: C:>ch14_02 No worries! However, if you take a look at ch14_02.exe in ILDASM, you can see the new Author attributes, as shown in Figure 14.1. Figure 14.1. A custom attribute in ILDASM.
In fact, there's a way for code to read its own metadata of the type stored in this custom attributeyou can use reflection. |