C#反射DeBugInfo

using System;using System.Reflection;namespace BugFixApplication {//a custom attribute BugFix to be//assigned to a class and its members

编程学习网为您整理以下代码实例,主要实现:C#反射DeBugInfo,希望可以帮到各位朋友。

using System;
using System.Reflection;

namespace BUGFixApplication {
   //a custom attribute BUGFix to be
   //assigned to a class and its members
   [AttributeUsage(AttributeTargets.Class |
   AttributeTargets.Constructor |
   AttributeTargets.FIEld |
   AttributeTargets.Method |
   AttributeTargets.Property,
   AllowMultiple = true)]

   public class DeBUGInfo : System.Attribute {
      private int BUGNo;
      private string developer;
      private string lastRevIEw;
      public string message;

      public DeBUGInfo(int bg, string dev, string d) {
         this.BUGNo = bg;
         this.developer = dev;
         this.lastRevIEw = d;
      }

      public int BUGNo {
         get {
            return BUGNo;
         }
      }

      public string Developer {
         get {
            return developer;
         }
      }

      public string LastRevIEw {
         get {
            return lastRevIEw;
         }
      }

      public string Message {
         get {
            return message;
         }
         set {
            message = value;
         }
      }
   }
   [DeBUGInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
   [DeBUGInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]

   class Rectangle {
      //member variables
      protected double length;
      protected double wIDth;
      public Rectangle(double l, double w) {
         length = l;
         wIDth = w;
      }

      [DeBUGInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
      public double GetArea() {
         return length * wIDth;
      }

      [DeBUGInfo(56, "Zara Ali", "19/10/2012")]
      public voID display() {
         Console.Writeline("Length: {0}", length);
         Console.Writeline("WIDth: {0}", wIDth);
         Console.Writeline("Area: {0}", GetArea());
      }
   }//end class Rectangle

   class ExecuteRectangle {
      static voID Main(string[] args) {
         Rectangle r = new Rectangle(4.5, 7.5);
         r.display();
         Type type = typeof(Rectangle);

         //iterating through the attribtues of the Rectangle class
         foreach (Object attributes in type.GetCustomAttributes(false)) {
            DeBUGInfo dbi = (DeBUGInfo)attributes;

            if (null != dbi) {
               Console.Writeline("BUG no: {0}", dbi.BUGNo);
               Console.Writeline("Developer: {0}", dbi.Developer);
               Console.Writeline("Last RevIEwed: {0}", dbi.LastRevIEw);
               Console.Writeline("Remarks: {0}", dbi.Message);
            }
         }

         //iterating through the method attribtues
         foreach (MethodInfo m in type.getmethods()) {

            foreach (Attribute a in m.GetCustomAttributes(true)) {
               DeBUGInfo dbi = (DeBUGInfo)a;

               if (null != dbi) {
                  Console.Writeline("BUG no: {0}, for Method: {1}", dbi.BUGNo, m.name);
                  Console.Writeline("Developer: {0}", dbi.Developer);
                  Console.Writeline("Last RevIEwed: {0}", dbi.LastRevIEw);
                  Console.Writeline("Remarks: {0}", dbi.Message);
               }
            }
         }
         Console.Readline();
      }
   }
}

本文标题为:C#反射DeBugInfo

上一篇: C#属性条件
下一篇: C#属性访问器

基础教程推荐