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

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

Версия 15:31, 26 мая 2010

Create generic delegate from reflection

using System;
using System.Reflection;
using System.Collections.Generic;
delegate void ComputeDelegate<T>( T instance, Decimal percent );
public class Employee
{
    public Decimal Salary;
    public Employee( Decimal salary ) {
        this.Salary = salary;
    }
    public void ComputeSalary( Decimal percent ) {
        Salary *= (1 + percent);
    }
}
public class MainClass
{
    static void Main() {
        List<Employee> employees = new List<Employee>();
        employees.Add( new Employee(40) );
        employees.Add( new Employee(65) );
        employees.Add( new Employee(95) );
        MethodInfo mi = typeof(Employee).GetMethod( "ComputeSalary", BindingFlags.Public | BindingFlags.Instance );
        ComputeDelegate<Employee> applyRaise = (ComputeDelegate<Employee> )Delegate.CreateDelegate(typeof(ComputeDelegate<Employee>), mi );
        foreach( Employee e in employees ) {
            applyRaise( e, (Decimal) 0.10 );
            Console.WriteLine( e.Salary );
        }
    }
}
44.0
71.5
104.5

delegate constaints

using System;
public delegate R Operation<T1, T2, R>( T1 val1, T2 val2 )
    where T1: struct
    where T2: struct
    where R:  struct;
public class MainClass
{
    public static double Add( int val1, float val2 ) {
        return val1 + val2;
    }
    static void Main() {
        Operation<int, float, double> op = new Operation<int, float, double>( Add );
        Console.WriteLine( "{0} + {1} = {2}", 1, 3.2, op(1, 3.2f) );
    }
}
1 + 3.2 = 4.20000004768372

Generic Delegate

using System;
using System.Collections.Generic;
delegate void FunctionToCall<T>(T value);                  
class MainClass
{
   static public void PrintString(string s)            
   {
      Console.WriteLine(s);
   }
   static public void PrintUpperString(string s)       
   {
      Console.WriteLine("{0}", s.ToUpper());
   }
   static void Main()
   {
      FunctionToCall<string> functionDelegate = PrintString;  
      functionDelegate += PrintUpperString;               
      functionDelegate("Hi There.");                              
   }
}
Hi There.
HI THERE.

Generic Delegate list

using System;
using System.Collections.Generic;
public delegate void MyDelegate<T>( T i );
public class DelegateList<T>
{
    public void Add( MyDelegate<T> del ) {
        imp.Add( del );
    }
    public void CallDelegates( T k ) {
        foreach( MyDelegate<T> del in imp ) {
            del( k );
        }
    }
    private List<MyDelegate<T> > imp = new List<MyDelegate<T> >();
}
public class MainClass
{
    static void Main() {
        DelegateList<int> delegates = new DelegateList<int>();
        delegates.Add( PrintInt );
        delegates.CallDelegates( 42 );
    }
    static void PrintInt( int i ) {
        Console.WriteLine( i );
    }
}
42

Return Type Inference With Multiple Returns

using System;
using System.ruponentModel;
    delegate T MyFunc<T>();
    class MainClass
    {
        static void WriteResult<T>(MyFunc<T> function)
        {
            Console.WriteLine(function());
        }
        static void Main()
        {
            WriteResult(delegate
            {
                if (1 > 2)
                {
                    return 10;
                }
                else
                {
                    return new object();
                }
            });
        }
    }