C#中的delegate委托类型基本学习教程

2019-12-30 11:35:55王冬梅

加上之前显示的静态 DelegateMethod,我们现在已有三个 Del 实例可以封装的方法。
调用时,委托可以调用多个方法。这被称为多播。若要向委托的方法列表(调用列表)添加其他方法,只需使用加法运算符或加法赋值运算符(“+”或“+=”)添加两个委托。例如:


MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = DelegateMethod;

//Both types of assignment are valid.
Del allMethodsDelegate = d1 + d2;
allMethodsDelegate += d3;

此时,allMethodsDelegate 的调用列表中包含三个方法,分别为 Method1、Method2 和 DelegateMethod。原有的三个委托(d1、d2 和 d3)保持不变。调用 allMethodsDelegate 时,将按顺序调用所有三个方法。如果委托使用引用参数,引用将按相反的顺序传递到所有这三个方法,并且一种方法进行的任何更改都将在另一种方法上见到。当方法引发未在方法内捕获到的异常时,该异常将传递到委托的调用方,并且不会调用调用列表中的后续方法。如果委托具有返回值和/或输出参数,它将返回上次调用方法的返回值和参数。若要删除调用列表中的方法,请使用减法运算符或减法赋值运算符(“+”或“+=”)。例如:


//remove Method1
allMethodsDelegate -= d1;

// copy AllMethodsDelegate while removing d2
Del oneMethodDelegate = allMethodsDelegate - d2;

由于委托类型派生自 System.Delegate,因此可以在委托上调用该类定义的方法和属性。例如,若要查询委托调用列表中方法的数量,你可以编写:


int invocationCount = d1.GetInvocationList().GetLength(0);

调用列表中具有多个方法的委托派生自 MulticastDelegate,该类属于 System.Delegate 的子类。由于这两个类都支持 GetInvocationList,因此在其他情况下,上述代码也将产生作用。
多播委托广泛用于事件处理中。事件源对象将事件通知发送到已注册接收该事件的接收方对象。若要注册一个事件,接收方需要创建用于处理该事件的方法,然后为该方法创建委托并将委托传递到事件源。事件发生时,源调用委托。然后,委托将对接收方调用事件处理方法,从而提供事件数据。给定事件的委托类型由事件源确定。有关详细信息,请参阅事件(C# 编程指南)。
在编译时比较分配的两个不同类型的委托将导致编译错误。如果委托实例是静态的 System.Delegate 类型,则允许比较,但在运行时将返回 false。例如:


delegate void Delegate1();
delegate void Delegate2();

static void method(Delegate1 d, Delegate2 e, System.Delegate f)
{
  // Compile-time error.
  //Console.WriteLine(d == e);

  // OK at compile-time. False if the run-time type of f 
  // is not the same as that of d.
  System.Console.WriteLine(d == f);
}