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

        employees.Add(new Employee(40000));
        employees.Add(new Employee(65000));
        employees.Add(new Employee(95000));

        // Create open instance delegate
        MethodInfo mi =
            typeof(Employee).GetMethod("ApplyRaiseOf",
                                       BindingFlags.Public |
                                       BindingFlags.Instance);
        ApplyRaiseDelegate <Employee> applyRaise =
            (ApplyRaiseDelegate <Employee>)
            Delegate.CreateDelegate(
                typeof(ApplyRaiseDelegate <Employee>),
                mi);

        // Apply raise.
        foreach (Employee e in employees)
        {
            applyRaise(e, (Decimal)0.10);

            // Send new salary to console.
            Console.WriteLine(e.Salary);
        }
    }
Пример #2
0
        public static void TestingDelegateSalaryRaiseOf()
        {
            Console.WriteLine("Open type: ");
            List <Employee> employees = new List <Employee>
            {
                new Employee(40000),
                new Employee(60000),
                new Employee(70000)
            };

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

            foreach (var e in employees)
            {
                applyRaise(e, (decimal)0.1);
                Console.WriteLine(e.Salary);
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            List <Employee> employees = new List <Employee>();

            employees.Add(new Employee(40000));
            employees.Add(new Employee(65000));
            employees.Add(new Employee(95000));

            // Создать делегат открытого экземпляра
            MethodInfo mi = typeof(Employee).GetMethod("ApplyRaiseOf", BindingFlags.Public | BindingFlags.Instance);
            ApplyRaiseDelegate <Employee> applyRaise =
                (ApplyRaiseDelegate <Employee>)
                Delegate.CreateDelegate(typeof(ApplyRaiseDelegate <Employee>), mi);

            // Выполнить повышение
            foreach (Employee e in employees)
            {
                applyRaise(e, (decimal)0.01);
                Console.WriteLine(e.Salary); // Вывести значение новой зарплаты на консоль
            }

            Console.ReadKey();
        }