Пример #1
0
 public void DemoNotification(string s, DelegateForNotification outputFunction)
 {
     Console.WriteLine("we're going to process the string:");
     if (outputFunction != null)
     {
         Console.WriteLine("Function that is used as delegate is: {0}", s);
         Console.WriteLine("Delegate output is: {0}", outputFunction);
     }
     else
         Console.WriteLine("We are sorry, but no processing methods were registered!!!");
 }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("------------------Delegates----------------");
            Notificator notificator = new Notificator();
            NotificationHandler notificationHandler = new NotificationHandler();

            // calling static delegates
            notificator.DemoNotification("static example", null); // nothing will happen here since we don't pass the parameter
            notificator.DemoNotification("static example", new
                DelegateForNotification(NotificationHandler.SendNotificationByInstantMessenger)); // nothing happening here
            notificator.DemoNotification("instance example", null);//nothing happen here we don't pass any handler
            notificator.DemoNotification("instance example", new DelegateForNotification(notificationHandler.SendNotificationByMail));
            notificator.DemoNotification("instance example", new DelegateForNotification(notificationHandler.SendNotificationByFax));
            NotificationHandler nullhandler = null;
            try
            {
                notificator.DemoNotification("instance example", new DelegateForNotification(nullhandler.SendNotificationByFax));
            }
            catch (System.ArgumentException e)
            {
                Console.WriteLine("We've tried to pass null object for delegate and catched: " + e.Message);
            }

            //------------------DELEGATES CHAIN-----------
            Console.WriteLine("-----------------------------DELEGATES CHAIN--------------------:");
            DelegateForNotification dlg1 = new DelegateForNotification(NotificationHandler.SendNotificationByInstantMessenger);
            DelegateForNotification dlg2 = new DelegateForNotification(notificationHandler.SendNotificationByMail);
            DelegateForNotification dlg3 = new DelegateForNotification(notificationHandler.SendNotificationByFax);

            DelegateForNotification dlg_chain = null;
            dlg_chain += dlg1;
            dlg_chain += dlg2;
            dlg_chain = (DelegateForNotification)Delegate.Combine(dlg_chain, dlg3);//more complex way to combine delegate to chain

            //you can view invocation list
            Delegate[] list = dlg_chain.GetInvocationList();
            foreach (Delegate del in list)
            {
                Console.WriteLine("Method: " + del.Method.ToString());
            }
            notificator.DemoNotification("chain example", dlg_chain);//calling notification to chain from 3 functions
            Console.ReadKey();
        }