Creating Custom Attributes

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:

  • validon Gives the language element(s) with which the attribute can be used. This item is a combination of AttributeTargets values (see Table 14.1), OR ed together. The default value is AttributeTargets.All .

  • allowmultiple (optional) If true , the attribute is multiuse , which means you can use it multiple times for the same element. Default is false ( single-use ).

  • inherited (optional) If true , the attribute is inherited by derived classes. The default is false , which means not inherited.

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.

graphics/14fig01.jpg

In fact, there's a way for code to read its own metadata of the type stored in this custom attributeyou can use reflection.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net