一文说通Dotnet的委托

简单的概念,也需要经常看看。

一、前言

先简单说说Delegate的由来。最早在C/C++中,有一个概念叫函数指针。其实就是一个内存指针,指向一个函数。调用函数时,只要调用函数指针就可以了,至于函数本身的实现,可以放在其它地方,也可以后实现。到了.Net,没有指针的概念了,但这种方式很实用,所以这个概念也保留了下来,形成了现在的委托Delegate。

另外,在.Net中,也把委托延伸了,与执行回调设计成了同一种机制,允许开发者定义回调的签名和类型。

当我们声明一个委托时,编译器会生成一个从MulticastDelegate派生的类。MulticastDelegate还包含几个方法,不过因为这些方法是CLR运行时动态生成的,代码IL中看不到,也不需要关心。

委托最大的特性是不需要进行强耦合。所以调用者其实并不知道所调用的是静态方法还是实例方法,也不知道具体调用的内容。举个见的例子,UI编程中的按钮Button类。按钮类本身并不知道它的OnClick事件是如何处理的,也不需要知道。所以实际中,OnClick事件就是使用委托发布的。开发者在开发过程中实现OnClick事件的处理,并由UI订阅使用。

这种方式,就是委托对类的解耦。

    为了防止不提供原网址的转载,特在这里加上原文链接:https://www.cnblogs.com/tiger-wang/p/14330314.html

二、简单委托

委托有一个非常简单的规则,就是:要引用的方法的返回类型或参数要与委托类型声明相匹配。

听着有点绕口,我们拿一个例子来说。

我们有一个方法:

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 = Sum;
        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;等同于挨个调用SumSubdelegate4 = delegate3 - delegate2;等同于调用(Sum + Sub) - Sub,实际最后调用的是Sum

五、结论

委托在Dotnet里,是一个很常用的代码组成。用好委托,可以很漂亮地实现诸如事件、回调等操作,所以必须要熟练。

最后再说一下委托的基本内容:

  • 委托是面向对象的操作,类型安全,数据安全;
  • 委托派生自Dotnet的Delegate类,它是一个类;
  • 委托类型是密封(sealed)的,所以不能从委托继承。

微信公众号:老王Plus

扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送

本文版权归作者所有,转载请保留此声明和原文链接

posted @ 2021-01-27 09:09  老王Plus  阅读(862)  评论(5编辑  收藏  举报