1. 程式人生 > >深入淺出Attribute (上)——Attribute初體驗

深入淺出Attribute (上)——Attribute初體驗

//====水之真諦====//
//上善若水,潤物無聲//
/* [url]http://blog.csdn.net/FantasiaX[/url]  */

//#define NOBUG
#define BUGED  // C#的巨集定義必須出現在所有程式碼之前。當前我們只讓BUGED巨集有效。
//#define LI
//#define ZHANG

using System;
using System.Diagnostics; // 注意:這是為了使用包含在此名稱空間中的ConditionalAttribute特性

namespace AttributeSample
{
         class ToolKit
         {
                   [ConditionalAttribute
("LI")]                                           // Attribute名稱的長記法
                   [ConditionalAttribute("BUGED")]
                   public static void Method1() { Console.WriteLine("Created By Li, Buged."); }

                   [ConditionalAttribute("LI")]
                   [ConditionalAttribute
("NOBUG")]
                   public static void Method2() { Console.WriteLine("Created By Li, NoBug."); }

                   [Conditional("ZHANG")]                                               // Attribute名稱的短記法
                   [Conditional("BUGED")]
                   public static void Method3() { Console
.WriteLine("Created By Zhang, Buged."); }

                   [Conditional("ZHANG")]
                   [Conditional("NOBUG")]
                   public static void Method4() { Console.WriteLine("Created By Zhang, NoBug."); }
         }
         class Program
         {
                   static void Main(string[] args)
                   {
                            // 雖然方法都被呼叫了,但只有符合條件的才會被執行。
                            ToolKit.Method1();
                            ToolKit.Method2();
                            ToolKit.Method3();
                            ToolKit.Method4();
                   }
         }
}