一文说通Dotnet的委托
簡單的概念,也需要經??纯?。
?
一、前言
先簡單說說Delegate的由來。最早在C/C++中,有一個概念叫函數指針。其實就是一個內存指針,指向一個函數。調用函數時,只要調用函數指針就可以了,至于函數本身的實現,可以放在其它地方,也可以后實現。到了.Net,沒有指針的概念了,但這種方式很實用,所以這個概念也保留了下來,形成了現在的委托Delegate。
另外,在.Net中,也把委托延伸了,與執行回調設計成了同一種機制,允許開發者定義回調的簽名和類型。
當我們聲明一個委托時,編譯器會生成一個從MulticastDelegate派生的類。MulticastDelegate還包含幾個方法,不過因為這些方法是CLR運行時動態生成的,代碼IL中看不到,也不需要關心。
?
委托最大的特性是不需要進行強耦合。所以調用者其實并不知道所調用的是靜態方法還是實例方法,也不知道具體調用的內容。舉個常見的例子,UI編程中的按鈕Button類。按鈕類本身并不知道它的OnClick事件是如何處理的,也不需要知道。所以實際中,OnClick事件就是使用委托發布的。開發者在開發過程中實現OnClick事件的處理,并由UI訂閱使用。
這種方式,就是委托對類的解耦。
二、簡單委托
委托有一個非常簡單的規則,就是:要引用的方法的返回類型或參數要與委托類型聲明相匹配。
聽著有點繞口,我們拿一個例子來說。
我們有一個方法:
void?PrintInfo(string?message);按照規則,這個方法對應的委托方法可以寫成:
void?Delegate_PrintInfo(string?message);這樣,按照規則,委托使用時就可以寫成:
Delegate_PrintInfo?=?PrintInfo;這樣,當我們調用Delegate_PrintInfo("Hello WangPlus")的時候,實際執行的是PrintInfo("Hello WangPlus")了。
?
下面,我們來看看委托的聲明。
public?delegate?int?Delegate_Method(int?x,?int?y);??委托可以封裝任何方法。在上面這個例子里,我們接受兩個參數,并返回一個int值。
在這樣一個聲明中,delegate是一個關鍵詞,表明我們聲明的是一個委托。而其它部分,跟我們正常的代碼方式沒有任何區別。
多舉幾個例子看看:
public?delegate?void?Demo_Func1(string?para); public?delegate?ClassA?Demo_Func2(ClassB?para); private?delegate?StructA?Demo_Func3(int?para);除了delegate,其它內容跟正常方法沒有區別。
?
聲明有了,如何用呢?看例子:
class?Program {public?delegate?int?Delegate_Method(int?x,?int?y);static?void?Main(string[]?args){Delegate_Method?handler?=?SumMethod;int?result?=?handler(3,?4);}static?int?Sum(int?x,?int?y){return?x?+?y;} }這是一個簡單的例子。
我們先定義了一個委托,接受兩個參數,并返回int值。我希望這個委托調用下面的Sum方法,因此Sum方法和委托Delegate_Method的簽名(參數和返回值)兼容。這兒要注意理解這個兼容的概念,不是完全相同,是兼容。
?
再寫個稍微復雜一點的例子:
public?delegate?void?Delegate_Method(int?x,?int?y);class?ExampleClass {public?void?Sum(int?x,?int?y){Console.WriteLine(x?+?y);}public?void?Sub(int?x,?int?y){Console.WriteLine(x?-?y);} } class?Program {static?void?Main(string[]?args){ExampleClass?example?=?new?ExampleClass();Delegate_Method?delegate_1;Delegate_Method?delegate_2;delegate_1?=?example.Sum;delegate_2?=?example.Sub;delegate_1(100,?50);delegate_2(100,?50);} }如果第一個例子明白了,那這個例子也不難理解。
三、委托鏈
委托鏈的核心的維護一個可調用的委托列表。當調用列表時,列表中的所有委托都會被調用。同時,委托鏈可以使用操作符,用+來組合,用-來刪除。
看例子:
public?delegate?void?Delegate_Method(int?x,?int?y);class?ExampleClass {public?void?Sum(int?x,?int?y){Console.WriteLine(x?+?y);}public?void?Sub(int?x,?int?y){Console.WriteLine(x?-?y);} } class?Program {static?void?Main(string[]?args){ExampleClass?example?=?new?ExampleClass();Delegate_Method[]?delegate_list?=?new?Delegate_Method[]?{?example.Sum,?example.Sub?};Delegate_Method?delegate_chain?=?delegate_list[0]?+?delegate_list[1];delegate_chain(100,?50);} }在這個例子中,定義了一個委托數組,然后用+操作符組合這些方法。
Delegate_Method?delegate_chain?=?delegate_list[0]?+?delegate_list[1];?? Delegate_Method?delegate_chain1?=?delegate_chain?-?delegate_list[0];??上面兩行代碼,CLR將解釋為(Sum + Sub) - Sum,并只執行Sub方法。這是一個使用-操作符從委托鏈中移除委托的例子。
?
您還可以遍歷委托鏈:
public?delegate?void?Delegate_Method(int?x,?int?y);class?ExampleClass {public?void?Sum(int?x,?int?y){Console.WriteLine(x?+?y);}public?void?Sub(int?x,?int?y){Console.WriteLine(x?-?y);} } class?Program {static?void?Main(string[]?args){ExampleClass?example?=?new?ExampleClass();Delegate_Method[]?delegate_list?=?new?Delegate_Method[]?{?example.Sum,?example.Sub?};Delegate_Method?delegate_chain?=?delegate_list[0]?+?delegate_list[1];Delegate[]?delegates?=?delegate_chain.GetInvocationList();for?(int?i?=?0;?i?<?delegates.Length;?i++){Delegate_Method?_delegate?=?(Delegate_Method)delegates[i];_delegate(100,?50);}} }在這個例子中,使用了GetInvocationList方法獲取委托鏈中的所有委托。這個方法幫助我們引用委托鏈中的每個委托,我們也可以從委托鏈中以任何順序調用委托。
四、多播委托
委托在被調用時可以調用多個方法,這稱之為多播。委托對象的一個非常有用的屬性是,它們可以被分配給一個委托實例,以便使用+/-操作符進行多播。組合委托調用由它組成的多個委托。
多播委托時,只能組合相同類型的委托。操作符可用于從組合委托中增加/刪除委托組件。
此外,多播委托返回類型總是void。
class?Program {public?delegate?void?Delegate_Method(int?x,?int?y);public?static?void?Sum(int?i,?int?j){Console.WriteLine(i?+?j);}public?static?void?Sub(int?i,?int?j){Console.WriteLine(i?-?j);}static?void?Main(string[]?args){Delegate_Method?delegate1,?delegate2,?delegate3,?delegate4;delegate1?=?Sum;delegate2?=?Sub;delegate3?=?delegate1?+?delegate2;delegate3(100,?50);delegate4?=?delegate3?-?delegate2;delegate4(100,?50);} }這段代碼里,delegate3 = delegate1 + delegate2;等同于挨個調用Sum和Sub;delegate4 = delegate3 - delegate2;等同于調用(Sum + Sub) - Sub,實際最后調用的是Sum。
五、結論
委托在Dotnet里,是一個很常用的代碼組成。用好委托,可以很漂亮地實現諸如事件、回調等操作,所以必須要熟練。
最后再說一下委托的基本內容:
委托是面向對象的操作,類型安全,數據安全;
委托派生自Dotnet的Delegate類,它是一個類;
委托類型是密封(sealed)的,所以不能從委托繼承。
喜歡就來個三連,讓更多人因你而受益
總結
以上是生活随笔為你收集整理的一文说通Dotnet的委托的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net:设计一个web应用
- 下一篇: 分享干货:靠刷算法题,真的可以刷进大厂吗