• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

        第12章 委托[《.net框架程序設計》讀書筆記]

        第12章 委托[《.net框架程序設計》讀書筆記]

        更新時間:2022-05-16 文章作者:未知 信息來源:網絡 閱讀次數:

        第十二章 委托

        一、 委托的使用

        靜態委托和實例委托,使用方法類似,這里給出一個使用可變參數委托的例子:

        using System;



        public class DelCls

        {

        public delegate void DelDef(params string[] strParams);



        public static void CallDel(DelDef dd)

        {

        if(dd != null) //請務必在此處進行判斷,這是個好習慣

        {

        dd("Hello", "world");

        }

        }

        }



        public class DelIns

        {

        //聲明為private(私有)成員并不影響在類型內部使用委托

        private static void ClsCallStr(params string[] strParams) //類型方法

        {

        //將字符串數組并順序輸出

        foreach(string str in strParams)

        {

        Console.Write("{0} ", str);

        }

        Console.WriteLine();

        }



        public void InsCallStr(params string[] strParams) //實例方法

        {

        //將字符串數組并反序輸出

        for(int i = strParams.Length - 1; i >= 0; i --)

        {

        Console.Write("{0} ", strParams[i]);

        }



        Console.WriteLine();

        }



        public static void Main()

        {

        DelIns di = new DelIns();



        DelCls.DelDef dd = null;

        Console.WriteLine("combine two delegate:");

        dd += new DelCls.DelDef(DelIns.ClsCallStr);

        dd += new DelCls.DelDef(di.InsCallStr);

        DelCls.CallDel(dd);



        Console.WriteLine("remove the first delegate:");

        dd -= new DelCls.DelDef(DelIns.ClsCallStr);

        DelCls.CallDel(dd);

        }

        }



        /*運行結果

        combine two delegate:

        Hello world

        world Hello

        remove the first delegate:

        world Hello

        */

        在C#中使用委托方法:

        l 創建委托所使用的方法必須和委托聲明相一致(參數列表、返回值都一致)

        l 利用 +=、-=來進行委托的鏈接或取消鏈接或直接使用Delegate.Combine和Delegate.Remove方法來實現

        l 使用MulticastDelegate的實例方法GetInvocationList()來獲取委托鏈中所有的委托



        二、 委托揭秘

        所有的委托都繼承自MulticastDelegate,編譯器在編譯時刻為委托的聲明生成了一個完整的委托類,重點注意其中的一些成員:

        ü 構造函數,傳入委托的目標對象(實例)及指向回調方法的整數

        ü 繼承自MulticastDelegate的_target(System.Object)字段

        ü 繼承自MulticastDelegate的_methodPtr(System.Int32)字段

        ü 繼承自MulticastDelegate的_prev(System.MulticastDelegaet)字段

        ü 生成的與方法聲明相一致Invoke函數用以調用方法

        可利用MulticastDelegate中的Method及Target屬性來考察_methodPtr及_target字段的性質。

        關于編譯器生成的委托類及Invoke方法的調用情況,可通過使用ILDAsm.exe查看執行文件的IL代碼獲得

        將上例中類型DelIns中的Main方法作如下修改,以實驗GetInvocationList及MulticastDelegate中屬性的使用:

        public class DelIns

        {



        public static void Main()

        {



        Delegate[] arrDel = dd.GetInvocationList();

        foreach(DelCls.DelDef d in arrDel)

        {

        Console.WriteLine("Object type: {0}, Method name: {1}",

        (d.Target != null) ? d.Target.GetType().ToString() : "null",

        d.Method.Name);

        }



        }



        }

        /*運行結果



        Object type: null, Method name: ClsCallStr

        Object type: DelIns, Method name: InsCallStr



        */

        三、 委托判等

        首先判斷_methodPtr及_target字段是否相等,若不等則返回false;

        若相等,繼續判斷_prev是否為null(指向委托鏈頭部的委托),若為null,則相等返回true;

        若不等,繼而判斷委托鏈上所有委托對象,重復上述步驟。



        可見牽涉到委托鏈的時候是個遞歸判斷的過程。

        四、 委托鏈

        l 首先被加入到委托鏈中的委托位于委托鏈的尾部,但首先被調用,這是因為Invoke中利用遞歸對委托函數進行調用,這樣位于頭部的委托最后被調用。

        l 委托調用后的返回值,只是最后一次被調用方法的返回值,即委托鏈頭部委托的返回值

        l 每調用一次Remove方法只刪除匹配的第一個委托鏈

        五、 委托與反射

        以下是.net framework sdk文檔提供的Delegate.CreateDelegate方法列表:

        創建指定類型的委托以表示指定的靜態方法。

        [C#] public static Delegate CreateDelegate(Type, MethodInfo);

        創建指定類型的委托,該委托表示要對指定的類實例調用的指定實例方法。

        [C#] public static Delegate CreateDelegate(Type, object, string);

        創建指定類型的委托,該委托表示指定類的指定靜態方法。

        [C#] public static Delegate CreateDelegate(Type, Type, string);

        創建指定類型的委托,該委托表示要按指定的大小寫敏感度對指定類實例調用的指定實例方法。

        [C#] public static Delegate CreateDelegate(Type, object, string, bool);



        下面的示例演示了創建靜態方法委托、實例方法委托以及動態調用委托:

        using System;

        using System.Reflection;



        public class DelReflection

        {

        public delegate void GoGo(string strPam, Int32 nPam);



        public static void ClsGo(string strPam, Int32 nPam)

        {

        Console.WriteLine("In class, String:{0}, Int32:{1}", strPam, nPam);

        }



        public void InsGo(string strPam, Int32 nPam)

        {

        Console.WriteLine("In instance, String:{0}, Int32:{1}", strPam, nPam);

        }



        public static void Main()

        {

        Delegate d = null;



        d = Delegate.CreateDelegate(typeof(GoGo), typeof(DelReflection), "ClsGo");

        if(d != null)

        d.DynamicInvoke(new Object[]{"Hello", 45});



        DelReflection dr = new DelReflection();

        d = Delegate.CreateDelegate(typeof(GoGo), dr, "InsGo");

        if(d != null)

        d.DynamicInvoke(new Object[]{"Hello", 45});

        }

        }

        /*運行結果

        In class, String:Hello, Int32:45

        In instance, String:Hello, Int32:45

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 亚洲V无码一区二区三区四区观看| 高清免费久久午夜精品 | 亚洲综合自拍成人| 亚洲成A人片77777国产| 免费无码AV片在线观看软件| 国产高清不卡免费视频| 日日躁狠狠躁狠狠爱免费视频| 激情五月亚洲色图| 亚洲网址在线观看| 亚洲国产成人一区二区精品区| 亚洲国产成人久久一区WWW| 成年性午夜免费视频网站不卡| 37pao成人国产永久免费视频| 国产中文字幕在线免费观看| 一级特级aaaa毛片免费观看| 老司机午夜在线视频免费| 亚洲丁香婷婷综合久久| 亚洲成_人网站图片| 亚洲人成7777| 亚洲一区二区三区四区视频| 亚洲理论片在线中文字幕| 久久夜色精品国产噜噜噜亚洲AV | 91在线精品亚洲一区二区| 国产亚洲精品观看91在线| 亚洲免费日韩无码系列| 日韩免费福利视频| 成人在线视频免费| 成人午夜视频免费| 精品免费国产一区二区| 日韩免费高清视频网站| 国产成人免费a在线资源| 国产一区二区三区在线免费| 国产一级做a爱免费视频| 免费一级成人毛片| 亚洲欧洲国产成人综合在线观看 | 国产黄片不卡免费| 91精品全国免费观看青青| 国产免费一区二区视频| 亚欧免费一级毛片| 日本免费网站视频www区| 免费电视剧在线观看|