28.2. Another custom attribute example

Let's complicate things a bit more by writing another custom attribute – this time for a class only. Let's modify AttributeClass.cs to include two more attribute classes called StatusOfClassAttribute and BuggyAttribute:

  • StatusOfClassAttribute shall be a class-only attribute (you can only use it to tag a class, not a method nor any other class entity). This attribute will be used to denote if the class is completed, or still under development.

  • BuggyAttribute shall be a method-only attribute used by a team leader to tag poorly written methods from sleep-deprived developers. BuggyAttribute doesn't contain any fields within the attribute class.

Here's the new AttributeClass.cs:

 1: // AttributeClass.cs
 2: using System;
 3: // --------------------------------------------------
 4: [AttributeUsage(AttributeTargets.All)]
 5: public class AuthorAttribute:Attribute{
 6:
 7:   public string AuthorName;
 8:   public string LastEditDate;
 9:
10:   // constructor
11:   public AuthorAttribute(string authorName, string
                             lastEditDate){
12:     this.AuthorName = authorName;
13:     this.LastEditDate = lastEditDate;
14:   }
15: }
16: // --------------------------------------------------
17: [AttributeUsage(AttributeTargets.Class)]
18: public class StatusOfClassAttribute:Attribute{
19:
20:   public string Status;
21:
22:   // constructor
23:   public StatusOfClassAttribute(string status){
24:     this.Status = status;
25:   }
26: }
27: // --------------------------------------------------
28: [AttributeUsage(AttributeTargets.Method)]
29: public class BuggyAttribute:Attribute{
30: }

Let's change MyClass.cs by applying the two new custom attributes.

 1: // MyClass.cs
 2: using System;
 3:
 4: [StatusOfClass ("developmental")]
 5: public class MyClass{
 6:
 7:   [Author("Mok","25 Nov 02")]
 8:   public void DoSomething(){
 9:     // some code
10:   }
11:
12:   [Author("Mindy","25 Nov 02")]
13:   public void DoSomethingElse(){
14:     // some code
15:   }
16:
17:   [Buggy]
18:   [Author("Abigail","27 Nov 02")]
19:   public void DoNothing(){
20:     // some code
21:   }
22: }

Note that the DoNothing method (lines 19 – 21) has been marked using two different attribute specifications. This is perfectly legal. It also doesn't matter if you swap lines 17 and 18.

Test.cs is altered to show the status of the class and only those methods marked as buggy:

 1: // Test.cs
 2: using System;
 3: using System.Reflection;
 4:
 5: class TestClass{
 6:   public static void Main(){
 7:
 8:     Type type = typeof(MyClass);
 9:
10:     // show all class attributes for this class
11:     StatusOfClassAttribute statusAttr;
12:     Object []classAttr = type.GetCustomAttributes(true);
13:     for (int i=0; i<classAttr.Length; i++){
14:       statusAttr = (StatusOfClassAttribute)classAttr[i];
15:       Console.WriteLine("class status: " + statusAttr.Status);
16:     }
17:
18:     // show methods marked with BuggyAttribute
19:     object []methods = type.GetMethods();
20:     Attribute methAttr;
21:     Console.WriteLine("These are the buggy methods:");
22:
23:     foreach(MethodInfo method in methods){
24:       object []methAttrArr = method.GetCustomAttributes(true);
25:
26:       for (int i=0; i< methAttrArr.Length; i++){
27:         methAttr = (Attribute) methAttrArr [i];
28:         if (methAttr is BuggyAttribute){
29:           BuggyAttribute buggy = (BuggyAttribute)methAttr;
30:           Console.WriteLine(method.Name);
31:         }
32:       }
33:
34:     } // end foreach
35:   } // end Main
36: }

Output:

c:expt>test
class status: development
These are the buggy methods:
DoNothing

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.97.94