⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-18-01

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 18-01: Working with custom attributes

namespace Programming_CSharp
{
   using System;
   using System.Reflection;

   // create custom attribute to be assigned to class members
   [AttributeUsage(AttributeTargets.Class |
       AttributeTargets.Constructor |
       AttributeTargets.Field |
       AttributeTargets.Method |
       AttributeTargets.Property,
       AllowMultiple = true)]
   public class BugFixAttribute : System.Attribute
   {
      // attribute constructor for 
      // positional parameters
      public BugFixAttribute
         (int bugID, 
         string programmer, 
         string date)
      {
         this.bugID = bugID;
         this.programmer = programmer;
         this.date = date;
      }

      // accessor
      public int BugID
      {
         get
         {
            return bugID;
         }
      }

      // property for named parameter
      public string Comment
      {
         get
         {
            return comment;
         }
         set
         {
            comment = value;
         }
      }

      // accessor
      public string Date
      {
         get
         {
            return date;
         }
      }

      // accessor
      public string Programmer
      {
         get
         {
            return programmer;
         }
      }
        
      // private member data 
      private int     bugID;
      private string  comment;
      private string  date;
      private string  programmer;
   }


   // ********* assign the attributes to the class ********

   [BugFixAttribute(121,"Jesse Liberty","01/03/05")]
   [BugFixAttribute(107,"Jesse Liberty","01/04/05", 
       Comment="Fixed off by one errors")]
   public class MyMath
   {
        
      public double DoFunc1(double param1)
      {
         return param1 + DoFunc2(param1);           
      }

      public double DoFunc2(double param1)
      {           
         return param1 / 3;
      }
    
   }

   public class Tester
   {
      public static void Main()
      {
         MyMath mm = new MyMath();
         Console.WriteLine("Calling DoFunc(7). Result: {0}",
            mm.DoFunc1(7));
      }        
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -