예제 #1
0
        static void Main(string[] args)
        {
            Processor proc1 = new Processor(0.75);
            Processor proc2 = new Processor(0.83);

            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 = chain[i] as ProcessResults;
                if (current != null)
                {
                    accumulator += current(4, 5);
                }
            }

            Console.WriteLine("Output: {0}", accumulator);
            Console.ReadKey();
        }
예제 #2
0
        public static void TestingChain()
        {
            DelegateChain dc1 = new DelegateChain(1.5);
            DelegateChain dc2 = new DelegateChain(2.5);

            ProcessResults[] delegates =
            {
                dc1.Compute,
                dc2.Compute,
                DelegateChain.StaticCompute,
            };

            ProcessResults cheined = delegates[0] + delegates[1] + delegates[2];

            Delegate[] chain = cheined.GetInvocationList();
            double     acc   = 0;

            for (var i = 0; i < chain.Length; i++)
            {
                ProcessResults current = (ProcessResults)chain[i];
                acc += current(4, 5);
            }
            Console.WriteLine($"Accumulator: {acc}");

            double combined = cheined(4, 5);

            Console.WriteLine($"Combined: {combined}");
        }
    static void Main()
    {
        Processor proc1 = new Processor(0.75);
        Processor proc2 = new Processor(0.83);

        ProcessResults[] delegates = new ProcessResults[] {
            new ProcessResults(proc1.Compute),
            new ProcessResults(proc2.Compute),
            new ProcessResults(Processor.StaticCompute)
        };

        ProcessResults chained = (ProcessResults)Delegate.Combine(delegates);

        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);
    }