Beispiel #1
0
        private void buttonMulticasting_Click(object sender, EventArgs e)
        {
            Console.WriteLine("\n**** Multi Casting with Delegates Demo ****\n");

            MethodPointer1 objMP_D = MyMethodPointerDemoClass.StaticMethod;//Don't use the "+" sign for the first one
            objMP_D += new MyMethodPointerDemoClass().InstanceMethod; //Use the "+" sign to connect additional ones

            // You can fire all methods with one call if the underlining invisible Delegate class 
            // supports it. Since .NET 4.0 delegates inherit from System.MulticastDelegate
            // expect them to multicast.
            Console.WriteLine(objMP_D.Invoke("\nCalling only the last method hooked up!\n"));

            //If not, then you execute non-multicast delegates like this...
            Console.WriteLine("\nUsing a loop to execute all methods\n");
            if (objMP_D != null)
            {
                foreach (MethodPointer1 p in objMP_D.GetInvocationList())
                {
                    Console.WriteLine("\t" + p.Invoke("Now Calling => " + p.Method.Name));
                }
            }
        }