一文说通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

扫描二维码,关注我的公众号,能够第一时间获得最新的我的文章和内容推送

本文版权归做者全部,转载请保留此声明和原文连接

相关文章
相关标签/搜索