A15_特性


/*
 * 
 * Obsolete
 * 
 * 
 * 
 * **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;
using System.Diagnostics;

namespace A15_Attribute
{
    class Demo1
    {
        //[Obsolete("該方法已過時,請使用NewMethod()替換",true)] //表示必須替換
        [Obsolete("該方法已過時,請使用NewMethod()替換")]
        public void OldMethod()
        {
            Console.WriteLine("這是舊方法");
        }

        public void NewMethod()
        {
            Console.WriteLine("這是新方法");
        }

        static void Main1(string[] args)
        {
            Demo1 obj = new Demo1();
            obj.OldMethod();
            obj.NewMethod();

        }
    }
}


/*
 * 
 * 實現條件編譯
 * 1.使用Condition特性,針對方法
 * 
 * 2.使用預編譯指令,非常靈活,直接影響所包含的所有行代碼
 * 
 * **/
//#define OnlyTest  //允許測試方法運行
//#define debug
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;
using System.Diagnostics;




namespace A15_Attribute
{

    
    class Demo2
    {
        [Conditional("OnlyTest")]//條件執行,默認不執行
        public void TestMethod()
        {
            Console.WriteLine("這是測試方法");
        }

        public void Method1()
        {
            Console.WriteLine("這是普通方法1");
        }
        public void Method2()
        {
            Console.WriteLine("這是普通方法2");
        }

        public void Test()
        {
            TestMethod();
            Method1();
            TestMethod();
            Method2();
        }

        //條件編譯
        public void Test2()
        {
            Console.WriteLine("aaa");
            Console.WriteLine("bbb");
            Console.WriteLine("ccc");
#if debug   //條件編譯 如果定義了宏debug 則執行
            Console.WriteLine("ddd");
#else
            Console.WriteLine("eee");
#endif
        }

        static void Main(string[] args)
        {
            Demo2 obj = new Demo2();
            obj.Test();
            obj.Test2();
        }
    }
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章