Sometimes invoking a delegate chain requires getting the return value of each call in the delegate chain, which is the time to call system. The Getinvocation method provided by the delegate class is used to obtain a set of delegates, with the following examples:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication2
{
Public delegate double ProcessResults (double x,double y);
public class Processor
{
Public Processor (double factor)
{
This.factor=factor;
}
Public double Compute (double x,double y)
{
Double Result=factor;
Console.WriteLine ("{0}", result);
return result;
}
public static double Staticcompute (double x,double y)
{
Double result= (x+y) *0.5;
Console.WriteLine ("{0}", result);
return result;
}
Private double factor;
}
Class Program
{
static void Main (string[] args)
{
Processor proc1 = new Processor (0.2);
Processor proc2 = new Processor (0.5);
Processresults[] Delegates = new processresults[]{
Proc1.compute,
Proc2.compute,
Processor.staticcompute};
ProcessResults chained = delegates[0] + delegates[1] + delegates[2];
Delegate[] chain = chained. Getinvocationlist ();
Double accumulator = 0;
for (int i = 0; i < chain. Length; i++)
{
ProcessResults current = (processresults) chain[i];
Accumulator + = current (4,5);
}
Console.WriteLine ("Output:{0}", accumulator);
Console.readkey ();
}
}
}
Iterative delegation Chain