Iterative delegation Chain

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.