一文說通Dotnet的委託

簡單的概念,也須要常常看看。html

1、前言

先簡單說說Delegate的由來。最先在C/C++中,有一個概念叫函數指針。其實就是一個內存指針,指向一個函數。調用函數時,只要調用函數指針就能夠了,至於函數自己的實現,能夠放在其它地方,也能夠後實現。到了.Net,沒有指針的概念了,但這種方式很實用,因此這個概念也保留了下來,造成了如今的委託Delegate。編程

另外,在.Net中,也把委託延伸了,與執行回調設計成了同一種機制,容許開發者定義回調的簽名和類型。c#

當咱們聲明一個委託時,編譯器會生成一個從MulticastDelegate派生的類。MulticastDelegate還包含幾個方法,不過由於這些方法是CLR運行時動態生成的,代碼IL中看不到,也不須要關心。數組

委託最大的特性是不須要進行強耦合。因此調用者其實並不知道所調用的是靜態方法仍是實例方法,也不知道具體調用的內容。舉個見的例子,UI編程中的按鈕Button類。按鈕類自己並不知道它的OnClick事件是如何處理的,也不須要知道。因此實際中,OnClick事件就是使用委託發佈的。開發者在開發過程當中實現OnClick事件的處理,並由UI訂閱使用。安全

這種方式,就是委託對類的解耦。微信

    爲了防止不提供原網址的轉載,特在這裏加上原文連接:https://www.cnblogs.com/tiger-wang/p/14330314.html函數

2、簡單委託

委託有一個很是簡單的規則,就是:要引用的方法的返回類型或參數要與委託類型聲明相匹配。設計

聽着有點繞口,咱們拿一個例子來講。指針

咱們有一個方法:code

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);
    }
}

若是第一個例子明白了,那這個例子也不難理解。

3、委託鏈

委託鏈的核心的維護一個可調用的委託列表。當調用列表時,列表中的全部委託都會被調用。同時,委託鏈可使用操做符,用+來組合,用-來刪除。

看例子:

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方法獲取委託鏈中的全部委託。這個方法幫助咱們引用委託鏈中的每一個委託,咱們也能夠從委託鏈中以任何順序調用委託。

4、多播委託

委託在被調用時能夠調用多個方法,這稱之爲多播。委託對象的一個很是有用的屬性是,它們能夠被分配給一個委託實例,以便使用+/-操做符進行多播。組合委託調用由它組成的多個委託。

多播委託時,只能組合相同類型的委託。操做符可用於從組合委託中增長/刪除委託組件。

此外,多播委託返回類型老是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;等同於挨個調用SumSubdelegate4 = delegate3 - delegate2;等同於調用(Sum + Sub) - Sub,實際最後調用的是Sum

5、結論

委託在Dotnet裏,是一個很經常使用的代碼組成。用好委託,能夠很漂亮地實現諸如事件、回調等操做,因此必需要熟練。

最後再說一下委託的基本內容:

  • 委託是面向對象的操做,類型安全,數據安全;
  • 委託派生自Dotnet的Delegate類,它是一個類;
  • 委託類型是密封(sealed)的,因此不能從委託繼承。

微信公衆號:老王Plus

掃描二維碼,關注我的公衆號,能夠第一時間獲得最新的我的文章和內容推送

本文版權歸做者全部,轉載請保留此聲明和原文連接

相關文章
相關標籤/搜索