Create Delegate using reflection : Delegate « Reflection « C# / CSharp Tutorial






using System;
using System.Reflection;
using System.Collections.Generic;

delegate void ComputeDelegate( Employee emp, Decimal percent );

public class Employee
{
    public Decimal Salary;

    public Employee( Decimal salary ) {
        this.Salary = salary;
    }

    public void ApplyRaiseOf( Decimal percent ) {
        Salary *= (1 + percent);
    }
}

public class MainClass
{
    static void Main() {
        List<Employee> employees = new List<Employee>();

        employees.Add( new Employee(20) );
        employees.Add( new Employee(50) );

        MethodInfo mi = typeof(Employee).GetMethod( "ApplyRaiseOf", BindingFlags.Public | BindingFlags.Instance );
        ComputeDelegate applyRaise = (ComputeDelegate ) Delegate.CreateDelegate( typeof(ComputeDelegate), mi );

        foreach( Employee e in employees ) {
            applyRaise( e, (Decimal) 0.10 );

            Console.WriteLine( e.Salary );
        }
    }
}
22.0
55.0








19.8.Delegate
19.8.1.Create Delegate using reflection
19.8.2.Get event from Type
19.8.3.Get/set private field using InvokeMember