private static void SafeInvokeDelegate(SimpleDelegate x)
 {
     foreach (var target in x.GetInvocationList()) {
         try {
             target.DynamicInvoke();
         } catch (Exception e) {
             Log.Logger.ErrorException("Exception invoking delegate", e);
         }
     }
 }
예제 #2
0
 private static void SafeInvokeDelegate(SimpleDelegate x)
 {
     foreach (var target in x.GetInvocationList())
     {
         try {
             target.DynamicInvoke();
         } catch (Exception e) {
             Log.Logger.ErrorException("Exception invoking delegate", e);
         }
     }
 }
예제 #3
0
        static void Main(string[] args)
        {
            #region TestDelegate
            //MyDelegate a, b, c, d;
            //a = new MyDelegate(Hello);
            //b = new MyDelegate(Bye);

            //c = a + b;

            //d = c - a;
            //Console.WriteLine("Invoke a: ");
            //a("A");
            //Console.WriteLine("Invoke b: ");
            //b("B");
            //Console.WriteLine("Invoke c: ");
            //c("C");
            //Console.WriteLine("Invoke d: ");
            //d("D");
            #endregion

            // багатоадресність делегата
            #region Test multicast
            //MyDelegate test = Hello;
            //test += Bye;
            //test("test");  //
            //Console.WriteLine();

            //test -= Hello;
            //test -= Hello;
            //test?.Invoke("After remove");
            //Console.WriteLine();
            //test += Hello;
            //test += GoodEvening;
            //test += Hello;
            //test += Bye;
            //test -= Hello;
            //test("Test");   // ? hello goodevening  bye
            #endregion

            #region Test multicast with return value
            SimpleDelegate del = Foo;
            del += Bar;
            del += Foo;
            Console.WriteLine(del(4));
            Console.WriteLine();
            foreach (Delegate item in del.GetInvocationList())// масив делегатів
            {
                Console.WriteLine(item.DynamicInvoke(4));
            }
            #endregion
        }