Beispiel #1
0
 /// <summary>
 /// 注册汽车引擎事件
 /// </summary>
 /// <param name="handler"></param>
 public void RegisterWithCarEngine(CarEngineHandler handler)
 {
     if (handler != null)
     {
         this.handler = handler;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Generic Event Handler Delegate Usage
        /// </summary>
        private void GenericEventHandlerDelegateUsage()
        {
            Console.WriteLine("=> Generic Event Handler Delegate Usage: ");

            CarWithGenericEventHandler c1 = new CarWithGenericEventHandler("SlugBug", 100, 10);

            // Register event handlers
            c1.AboutToBlow += CarIsAlmostDoomed;
            c1.AboutToBlow += CarAboutToBlow;

            CarEngineHandler <CarEventArgs> d = new CarEngineHandler <CarEventArgs>(CarExploded);

            c1.Exploded += d;

            Console.WriteLine("Speed Up");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Remove CarExploded method from invocation list
            c1.Exploded -= CarExploded;

            Console.WriteLine("Speed Up");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine();
        }
Beispiel #3
0
            // Додавання методу для доступу до змінної listOfHandlers ззовні
            public void RegisterWithCarEngine(CarEngineHandler metodToCall)
            {
                // Змінній типу делегат присвоюємо метод, що має сигнатуру, яка вказана при оголошенні делегата

                listOfHandlers += metodToCall;
                // Змінений оператор
            }
Beispiel #4
0
 public void UnRegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     if (methodToCall != null)
     {
         listOfHandlers -= methodToCall;
     }
 }
Beispiel #5
0
 public void UnRegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     //_listOfHandlers -= methodToCall;
     if (_listOfHandlers.GetInvocationList().Contains(methodToCall))
     {
         _listOfHandlers = Delegate.Remove(_listOfHandlers, methodToCall) as CarEngineHandler;
     }
 }
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     if (_listOfHandlers == null)
     {
         Console.WriteLine("_listOfHandlers is null!");
     }
     _listOfHandlers += methodToCall;
 }
Beispiel #7
0
        // Add registeration function for the caller
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            // Single cast
            // listOfHandlers = methodToCall;

            // Multicast using += operator
            listOfHandlers += methodToCall;
        }
Beispiel #8
0
 //step 3: Add a registration function for a caller
 public void RegisterWithCarEngine(CarEngineHandler handler)
 {
     //if (listOfHandlers == null)
     //    listOfHandlers = handler;
     //else
     //    Delegate.Combine(listOfHandlers, handler);
     listOfHandlers += handler;
 }
Beispiel #9
0
        public void RegisterWithCarEngine(CarEngineHandler methodToCall) {
            //listOfHandlers = methodToCall;
            listOfHandlers += methodToCall;  //multicast works fine, equal to code below
        /*    if (listOfHandlers == null) {
                listOfHandlers = methodToCall;
            } else {
                Delegate.Combine(listOfHandlers, methodToCall);
            }*/

        }
Beispiel #10
0
 public void RegisterWithCarEngine(CarEngineHandler methodToCall) 
 {
     //去调用Delegate.Comine()方法
     listOfHandlers += methodToCall;
     // 等价于
     //if (listOfHandlers == null)
     //    listOfHandlers = methodToCall;
     //else
     //    Delegate.Combine(listOfHandlers, methodToCall);
 }
Beispiel #11
0
        // 3) Add registration function for the caller.
        // Now with multicasting support!
        // Note we are now using the += operator, not
        // the assignment operator (=).
        //public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        //{
        //    listOfHandlers += methodToCall;
        //}

        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            if (listOfHandlers == null)
            {
                listOfHandlers = methodToCall;
            }
            else
            {
                Delegate.Combine(listOfHandlers, methodToCall);
            }
        }
        // 3) Add registration function for the caller.
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            // Code dibawah ini tidak berjalan semestinya
            //if (listOfHandlers == null)
            //    listOfHandlers = methodToCall;
            //else
            //    Delegate.Combine(listOfHandlers, methodToCall);

            // Pengganti menggunakan multicasting delegate
            listOfHandlers += methodToCall;
        }
        // 3) Add registration function for the caller.
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            // Code dibawah ini tidak berjalan semestinya
            //if (listOfHandlers == null)
            //    listOfHandlers = methodToCall;
            //else
            //    Delegate.Combine(listOfHandlers, methodToCall);

            // Pengganti menggunakan multicasting delegate
            listOfHandlers += methodToCall;
        }
Beispiel #14
0
 //Add registration function for the caller
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     if (listOfHandler == null)
     {
         listOfHandler = methodToCall;
     }
     else
     {
         listOfHandler += methodToCall;
     }
 }
 /// <summary>
 /// Add registration function for the caller. Note use of method group here
 /// </summary>
 /// <param name="methodToCall">Delegate to register</param>
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     if (listOfHandlers == null)
     {
         listOfHandlers = methodToCall;   // start the list
     }
     else
     {
         listOfHandlers += methodToCall; // add to the list
     }
 }
Beispiel #16
0
        // 3) Add registration function for the caller.
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            // listOfHandlers = methodToCall;
            // listOfHandlers += methodToCall;
            listOfHandlers += methodToCall;

            /*if (listOfHandlers == null)
             *  listOfHandlers = methodToCall;
             * else
             *  Delegate.Combine(listOfHandlers, methodToCall);*/
        }
Beispiel #17
0
        /// <summary>
        /// (3) Add registration function for the caller.
        /// </summary>
        /// <param name="methodToCall"></param>
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            listOfHandlers += methodToCall;

            /*
             *  Rather than using the += operator, we could call 'Delegate.Combine'
             *  if (listOfHandlers == null)
             *      listOfHandlers = methodToCall;
             *  else
             *      Delegate.Combine(listOfHandlers, methodToCall);
             */
        }
Beispiel #18
0
        // Step III
        public void RegisterWithCarEngine(CarEngineHandler handler)
        {
            listOfHandlers += handler;

            // Or long winded

            // if (listOfHandlers == null) {
            //     listOfHandlers = handler;
            // } else {
            //     listOfHandlers = CarEngineHandler.Combine(listOfHandlers, handler) as CarEngineHandler;
            // }
        }
Beispiel #19
0
            // 3) Add registration function for the caller.
            // Now with multicasting support!
            // Note we are now using the += operator, not
            // the assignment operator (=).
            public void RegisterWithCarEngine(CarEngineHandler methodToCall)
            {
                //When you use the += operator on a delegate object, the compiler resolves this to a call on the static Delegate.Combine() method.
                //In fact, you could call Delegate.Combine() directly; however, the += operator offers a simpler alternative
                listOfHandlers += methodToCall;

                // calling Delegate.Combine()
                //if (listOfHandlers == null)
                //    listOfHandlers = methodToCall;
                //else
                //    Delegate.Combine(listOfHandlers, methodToCall);
            }
Beispiel #20
0
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     // handlersList += methodToCall;
     if (handlersList == null)
     {
         handlersList = methodToCall;
     }
     else
     {
         Delegate.Combine(handlersList, methodToCall);
     }
 }
        // 3) Add registration function for the caller.
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            //listOfHandlers = methodToCall;
            //listOfHandlers += methodToCall;

            if (listOfHandlers == null)
            {
                listOfHandlers = methodToCall;
            }
            else
            {
                listOfHandlers = Delegate.Combine(listOfHandlers, methodToCall) as CarEngineHandler;
            }
        }
Beispiel #22
0
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            //working
            //listOfHandlers += methodToCall;

            //not working
            if (listOfHandlers == null)
            {
                listOfHandlers = methodToCall;
            }
            else
            {
                Delegate.Combine(listOfHandlers, methodToCall);
            }
        }
Beispiel #23
0
        //3) add registration function for the caller
        public void RegisterWithCarEngine(CarEngineHandler methodToCall) {

            //using the "+=" syntax...
            listOfHandlers += methodToCall;//simply adding the "+" enables multi-casting(allowing
            //the delegate object to call multiple methods, it calls Delegate.Combine() behind the
            //scenes)
            

            ////using the "Delegate.Combine()" syntax...
            //if (listOfHandlers == null)
            //    listOfHandlers = methodToCall;
            //else
            //    Delegate.Combine(listOfHandlers, methodToCall);

        }
Beispiel #24
0
        //function for registering delegates
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            listOfHandlers += methodToCall;

            ////ridiculously obnoxious non-operator implementation
            //if (null == listOfHandlers)
            //{
            //    listOfHandlers = methodToCall;
            //}
            //else
            //{
            //    //must treat handler variable as variable, Combine() is static and takes a delegate type so methodToCall must be boxed into appropriate type,
            //    //    then the result of Combine() must be cast to correct derived delegate type.
            //    listOfHandlers = (CarEngineHandler)Delegate.Combine(listOfHandlers, new CarEngineHandler(methodToCall));
            //}
        }
Beispiel #25
0
        // 3) Add registration function for the caller.
        public void RegisterWithCarEngine(CarEngineHandler methodToCall)
        {
            //_listOfHandlers = methodToCall;

            // Now with multicasting support!
            // Note we are now using the += operator, not
            // the assignment operator (=).
            //_listOfHandlers += methodToCall;

            //Alternate method of registering multiple delegates
            if (_listOfHandlers == null)
            {
                _listOfHandlers = methodToCall;
            }
            else
            {
                _listOfHandlers = Delegate.Combine(_listOfHandlers, methodToCall) as CarEngineHandler;
            }
        }
Beispiel #26
0
            public static void TestCarDelegate()
            {
                Console.WriteLine("***** Delegates as event enablers *****\n");

                // First, make a Car object.
                Car c1 = new Car("SlugBug", 100, 10);

                c1.RegisterWithCarEngine(new CarEngineHandler(OnCarEngineEvent));
                // or like this
                // c1.RegisterWithCarEngine(OnCarEngineEvent);

                // This time, hold onto the delegate object,
                // so we can unregister later.
                Car.CarEngineHandler handler2 = new CarEngineHandler(OnCarEngineEvent2);
                c1.RegisterWithCarEngine(handler2);

                // Speed up (this will trigger the events).
                Console.WriteLine("***** Speeding up *****");
                for (int i = 0; i < 6; i++)
                {
                    c1.Accelerate(20);
                }

                // Unregister from the second handler.
                c1.UnRegisterWithCarEngine(handler2);
                // c1.UnRegisterWithCarEngine(CallMeHere);

                // We won’t see the "uppercase" message anymore!
                Console.WriteLine("***** Speeding up *****");
                for (int i = 0; i < 6; i++)
                {
                    c1.Accelerate(20);
                }

                Console.ReadLine();
            }
Beispiel #27
0
 public void RegisterCarEngine(CarEngineHandler methodToCall)
 {
     this.listOfhandlers += methodToCall;
 }
Beispiel #28
0
 public void UnregisterWithCarEngine(CarEngineHandler methodToCall) {
     if (methodToCall != null) {
         listOfHandlers -= methodToCall;
     }
 }
Beispiel #29
0
 // 3) Add registration function for the caller.
 public void RegisterWithCarEngine(CarEngineHandler methodToCall) {
     listOfHandlers += methodToCall;
 }
Beispiel #30
0
 public void UnregisterWithCarEngine(CarEngineHandler callback)
 {
     listOfHandlers -= callback; // allow multi-casting
 }
Beispiel #31
0
 public void UnRegisterWithCarEngine(CarEngineHandler methodToDelete)
 {
     listOfHandlers -= methodToDelete;
 }
Beispiel #32
0
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     _handlers = methodToCall;
 }
Beispiel #33
0
 public void Register(CarEngineHandler methodToCall)
 {
     listOFHandles = methodToCall;
 }
Beispiel #34
0
 // 3) Add registration function for the caller.
 public void RegisterWithCarEngine( CarEngineHandler methodToCall )
 {
     // listOfHandlers = methodToCall;
     // listOfHandlers += methodToCall;
     // listOfHandlers += methodToCall;
     if (listOfHandlers == null)
         listOfHandlers = methodToCall;
     else
         Delegate.Combine(listOfHandlers, methodToCall);
 }
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     listOfHandlers = methodToCall;
     listOfHandlers("Fine tooooooooooooooo");
     methodToCall("somay sobar porikha nei");
 }
Beispiel #36
0
 public void UnRegisterWithCarEngine (CarEngineHandler methodToCall) {
     Debug.Assert (_listOfHandlers != null, "_listOfHandlers != null");
     Debug.Assert (methodToCall != null, "methodToCall != null");
     _listOfHandlers -= methodToCall;
 }
Beispiel #37
0
 public void RegWithCarEngine(CarEngineHandler carEngHandler)
 {
     listOfHandlers = carEngHandler;
 }
Beispiel #38
0
 public void UnregisterWithCarEngine(CarEngineHandler handler)
 {
     listOfHandlers -= handler;
 }
Beispiel #39
0
 public void UnRegisterWithCarEngine(CarEngineHandler methodToCall) {            
     listOfHandlers -= methodToCall;  //Remove
 }
Beispiel #40
0
 //Удаляет функцию из списка
 public void UnRegisterWithCarEngine(CarEngineHandler methodtocall)
 {
     listOfHandlers -= methodtocall;
 }
Beispiel #41
0
 // 3) Add registration functions for the caller.
 public void RegisterWithCarEngine(CarEngineHandler methodToCall)
 {
     listOfHandlers += methodToCall;
 }
Beispiel #42
0
 public void RegisterCarEngine(CarEngineHandler methodCall)
 {
     listOfHundlers = methodCall;
 }