• <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

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

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 嫩草影院免费观看| 国产精品成人免费视频网站京东| 中国内地毛片免费高清| 日韩精品内射视频免费观看| 成年女人视频网站免费m| 国产日韩亚洲大尺度高清| 亚洲人成网站在线在线观看| 国产成人AV免费观看| 日韩特黄特色大片免费视频| 久久亚洲精品国产精品黑人| 国产亚洲精彩视频| 2021在线观看视频精品免费| 久久久久久亚洲精品不卡| 国产精品亚洲综合五月天| 99精品免费视频| 午夜亚洲福利在线老司机| 亚洲综合久久一本伊伊区| 久久不见久久见中文字幕免费 | 亚洲一区二区三区国产精品无码| 免费观看四虎精品成人| 亚欧免费无码aⅴ在线观看| 亚洲真人日本在线| 免费在线观看一区| 亚洲精品国产字幕久久不卡| 99久久久国产精品免费牛牛| 亚洲午夜在线播放| 亚洲av日韩片在线观看| 久久精品免费电影| 亚洲AV午夜成人影院老师机影院| 一级做受视频免费是看美女 | 亚洲一本到无码av中文字幕 | 96免费精品视频在线观看| 亚洲国产婷婷六月丁香| 日韩在线播放全免费| 亚洲精品视频在线观看视频| 三年片在线观看免费观看大全一| 亚洲专区一路线二| 亚洲性在线看高清h片| 黄色网址免费观看| 亚洲最大成人网色香蕉| 亚洲午夜激情视频|