Accessing Metadata

We'll start our discussion of reflection by extracting metadata from the Author attributes we created in the previous example. We can read those attributes at runtime with the System.Reflection.MemberInfo class's GetCustomAttributes method, which works like this:

 
 public abstract object[] GetCustomAttributes (Type  attributeType  , bool  inherit  ) 

Here are the arguments you pass to this method:

  • attributeType The type of attributes you want.

  • inherit Specifies whether to search the inheritance chain to find attributes.

For example, to access the properties of the [Author] attributes we created in ch14_02.cs, we can create a MemberInfo object and call that object's GetCustomAttributes method to get an array of Author attribute objects:

 
 public static void Main() {  object[] attributes;  Displayer displayer = new Displayer();   displayer.Display();  System.Reflection.MemberInfo memberInfo = typeof(Displayer);   attributes = memberInfo.GetCustomAttributes(typeof(Author), false);  .     .     . 

These Author attribute objects support the properties we've built into them Name , Time , and Text so we can loop over each Author attribute object and display those properties as you see in ch14_03.cs, Listing 14.3.

Listing 14.3 Reading Metadata (ch14_03.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_03 {   public static void Main()   {     object[] attributes;     Displayer displayer = new Displayer();     displayer.Display();     System.Reflection.MemberInfo memberInfo = typeof(Displayer);     attributes = memberInfo.GetCustomAttributes(typeof(Author), false);  foreach(Object attribute in attributes)   {   Author author = (Author) attribute;   System.Console.WriteLine("Author = {0}", author.Name);   System.Console.WriteLine("Time = {0}", author.Time);   System.Console.WriteLine("Text = {0}", author.Text);   System.Console.WriteLine();   }  } } 

Here's what you see when you run ch14_03, showing the attribute metadata as you see here:

 
 C:\>ch14_03 No worries! Author = Cary Grant Time = 11/25/48 Text = Author = Grace Kelly Time = 11/25/48 Text = Hi Cary! 

Next we're going to take a look at how to discover and examine types at runtime using 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