日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

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

第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

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

本類教程下載

系統下載排行

主站蜘蛛池模板: 成人免费公开视频 | 亚洲视频中文字幕在线观看 | 亚洲美女中文字幕 | 日本欧美韩国一区二区三区 | 欧美日韩一区二区三区在线观看 | 亚洲另类 专区 欧美 制服 | www视频免费在线观看 | 亚洲一区亚洲二区 | 欧美黑人巨大xxx猛交 | 波霸大乳人体xxxxx | 草草影院国产第一页 | 成人在线色视频 | 精品国产免费观看久久久 | 欧美久久一区二区三区 | 国产精品91在线 | 亚洲女人18毛片水真多 | 久久日本精品一区二区三区 | 国产日韩美国成人 | 日韩久久精品一区二区三区 | 成人拍拍拍在线观看 | 欧美日韩三 | 91免费视频 | 久久高清一区二区三区 | 国产成人在线影院 | 日本特一级片 | 人摸人人 | 美女搞逼网站 | 中文字幕26页 | 伊人小视频 | 日日噜噜夜夜狠视频免费 | 国产不卡福利 | 四色婷婷婷婷色婷婷开心网 | 亚洲一区二区免费 | 欧美一二三 | 亚洲综合成人在线 | 国产精品久久在线观看 | www.狠狠色| 免费91麻豆精品国产自产在线观看 | 亚洲国产精品一区二区九九 | 日韩久操| 香蕉久 |