Пример #1
0
        /// <summary>
        /// 委托链:使用Combine和Remove
        /// </summary>
        private static void ChainDelegateDemo1()
        {
            Console.WriteLine("-----  Chain delegate demo1 -----");
            Feedback     fb1 = new Feedback(FeedbackToConsole);
            Feedback     fb2 = new Feedback(FeedbackToMsgBox);
            DelegateDemo p   = new DelegateDemo();
            Feedback     fb3 = new Feedback(p.FeedbackToFile);


            Feedback fbchain = null;

            fbchain = (Feedback)Delegate.Combine(fbchain, fb1);
            fbchain = (Feedback)Delegate.Combine(fbchain, fb2);
            fbchain = (Feedback)Delegate.Combine(fbchain, fb3);

            Counter(1, 2, fbchain);



            fbchain = (Feedback)Delegate.Remove(fbchain, new Feedback(FeedbackToMsgBox));

            Counter(1, 2, fbchain);

            Console.WriteLine("----- end  Chain delegate demo1 -----");


            Console.WriteLine();
        }
Пример #2
0
        /// <summary>
        /// 委托链:使用+=和-=
        /// </summary>
        private static void ChainDelegateDemo2()
        {
            Console.WriteLine("-----  Chain delegate demo2 -----");
            Feedback     fb1 = new Feedback(FeedbackToConsole);
            Feedback     fb2 = new Feedback(FeedbackToMsgBox);
            DelegateDemo p   = new DelegateDemo();
            Feedback     fb3 = new Feedback(p.FeedbackToFile);


            Feedback fbchain = null;

            fbchain += fb1;
            fbchain += fb2;
            fbchain += fb3;

            Counter(1, 2, fbchain);



            fbchain -= FeedbackToMsgBox;

            Counter(1, 2, fbchain);

            Console.WriteLine("----- end  Chain delegate demo2 -----");


            Console.WriteLine();
        }
Пример #3
0
        /// <summary>
        /// 委托调用实例方法
        /// </summary>
        private static void InstanceDelegateDemo()
        {
            Console.WriteLine("----- Instance delegate demo -----");
            DelegateDemo p = new DelegateDemo();

            Counter(1, 3, new Feedback(p.FeedbackToFile));

            Console.WriteLine("----- end Instance delegate demo -----");
            Console.WriteLine();
        }