Csharp/CSharp Tutorial/delegate/Chained delegate — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

Chained Delegate

using System;
public delegate double ComputeDelegate( double x,double y );
public class MyClass
{
    public MyClass( ) {
    }
    public double Add( double x, double y ) {
        double result = x+y;
        Console.WriteLine( "InstanceResults: {0}", result );
        return result;
    }
    public static double Subtract( double x,double y ) {
        double result = x - y;
        Console.WriteLine( "StaticResult: {0}", result );
        return result;
    }
    private double factor;
}
public class MainClass
{
    static void Main() {
        MyClass proc1 = new MyClass( );
        MyClass proc2 = new MyClass( );
        ComputeDelegate[] delegates = new ComputeDelegate[] {
            new ComputeDelegate( proc1.Add ),
            new ComputeDelegate( proc2.Add ),
            new ComputeDelegate( MyClass.Subtract )
        };
        ComputeDelegate chained = (ComputeDelegate) Delegate.rubine( delegates );
        double combined = chained( 4, 5 );
        
        Console.WriteLine( "Output: {0}", combined );
    }
}
InstanceResults: 9
InstanceResults: 9
StaticResult: -1
Output: -1

Delegate.Combine

using System;
public delegate double ComputeDelegate( double x,double y );
public class MyClass
{
    public MyClass( ) {
    }
    public double Add( double x, double y ) {
        double result = x+y;
        Console.WriteLine( "InstanceResults: {0}", result );
        return result;
    }
    public static double Subtract( double x,double y ) {
        double result = x - y;
        Console.WriteLine( "StaticResult: {0}", result );
        return result;
    }
    private double factor;
}
public class MainClass
{
    static void Main() {
        MyClass proc1 = new MyClass( );
        MyClass proc2 = new MyClass( );
        ComputeDelegate[] delegates = new ComputeDelegate[] {
            new ComputeDelegate( proc1.Add ),
            new ComputeDelegate( proc2.Add ),
            new ComputeDelegate( MyClass.Subtract )
        };
        ComputeDelegate chained = (ComputeDelegate) Delegate.rubine( delegates );
        double combined = chained( 4, 5 );
        
        Console.WriteLine( "Output: {0}", combined );
    }
}
InstanceResults: 9
InstanceResults: 9
StaticResult: -1
Output: -1

Delegate: GetInvocationList

using System;
public delegate double Compute( double x, double y );
public class Processor
{
    public double Add( double x, double y ) {
        double result = x + y;
        Console.WriteLine( "InstanceResults: {0}", result );
        return result;
    }
    public static double Subtract( double x, double y ) {
        double result = x-y;
        Console.WriteLine( "StaticResult: {0}", result );
        return result;
    }
}
public class MainClass
{
    static void Main() {
        Processor proc1 = new Processor( );
        Processor proc2 = new Processor( );
        Compute[] delegates = new Compute[] {
            new Compute( proc1.Add ),
            new Compute( proc2.Add ),
            new Compute( Processor.Subtract )
        };
        Compute chained = (Compute) Delegate.rubine( delegates );
        Delegate[] chain = chained.GetInvocationList();
        
        
        for( int i = 0; i < chain.Length; ++i ) {
            Compute current = (Compute) chain[i];
            Console.WriteLine(current( 4, 5 ));
        }
        
        
    }
}
InstanceResults: 9
9
InstanceResults: 9
9
StaticResult: -1
-1