委托 多播委托 event 事件

委托  多播委托 意义

/多播委托有啥用呢?一个委托实例包含多个方法,可以通过+=/-=去增加/移除方法,Invoke时可以按顺序执行全部动作

//多播委托:任何一个委托都是多播委托类型的子类,可以通过+=去添加方法
//+= 给委托的实例添加方法,会形成方法链,Invoke时,会按顺序执行系列方法

      {
                //多播委托有啥用呢?一个委托实例包含多个方法,可以通过+=/-=去增加/移除方法,Invoke时可以按顺序执行全部动作

                //多播委托:任何一个委托都是多播委托类型的子类,可以通过+=去添加方法
                //+=  给委托的实例添加方法,会形成方法链,Invoke时,会按顺序执行系列方法
                Action method = this.DoNothing;
                method += this.DoNothing;
                method += DoNothingStatic;
                method += new Student().Study;
                method += Student.StudyAdvanced;
                //method.BeginInvoke(null, null);//启动线程来完成计算  会报错,多播委托实例不能异步
                foreach (Action item in method.GetInvocationList())
                {
                    item.Invoke();
                    item.BeginInvoke(null, null);
                }
                //method.Invoke();
                //-=  给委托的实例移除方法,从方法链的尾部开始匹配,遇到第一个完全吻合的,移除,且只移除一个,如果没有匹配,就啥事儿不发生
                method -= this.DoNothing;
                method -= DoNothingStatic;
                method -= new Student().Study;//去不掉  原因是不同的实例的相同方法,并不吻合
                method -= Student.StudyAdvanced;
                method.Invoke();
                //中间出现未捕获的异常,直接方法链结束了

            }
            {
                Func<int> func = this.Get;
                func += this.Get2;
                func += this.Get3;
                int iResult = func.Invoke();
                //结果是3  以最后一个为准,前面的丢失了。。所以一般多播委托用的是不带返回值的
            }

  

 

event意义

//事件event:一个委托的实例,带一个event关键字
//限制权限,只允许在事件声明类里面去invoke和赋值,不允许外面,甚至子类
//事件和委托的区别与联系?
//委托是一种类型,事件是委托类型的一个实例,加上了event的权限控制
//Student是一种类型,Tony就是Student类型的一个实例,

public event Action CatMiaoActionHandler;

//实例展示

    public class EventStandard
    {
        public static void Show()
        {
            Lesson lesson = new Lesson()
            {
                Id = 123,
                Name = "零基础到多项目实战班",
                Price = 2699
            };
            //订阅:把订户和发布者的事件关联起来
            lesson.IncreaseHandler += new Student().Buy;
            lesson.IncreaseHandler += new Tencent().Popularize;
            lesson.Price = 3999;
        }


        /// <summary>
        /// 订户:关注事件,事件发生后,自己做出对应的动作
        /// </summary>
        public class Student
        {
            public void Buy(object sender, EventArgs e)
            {
                Lesson lesson = (Lesson)sender;
                Console.WriteLine($"This is {lesson.Name} Lesson");

                XEventArgs args = (XEventArgs)e;
                Console.WriteLine($"之前价格{args.OldPrice}");
                Console.WriteLine($"现在价格{args.NewPrice}");
                Console.WriteLine("果断买了!!!");
            }
        }
        public class Tencent
        {
            public void Popularize(object sender, EventArgs e)
            {
                Lesson lesson = (Lesson)sender;
                Console.WriteLine($"This is {lesson.Name} Lesson");

                XEventArgs args = (XEventArgs)e;
                Console.WriteLine($"之前价格{args.OldPrice}");
                Console.WriteLine($"现在价格{args.NewPrice}");
                Console.WriteLine("广大用户请留意!!!");
            }
        }

        /// <summary>
        /// 事件参数  一般会为特定的事件去封装个参数类型
        /// </summary>
        public class XEventArgs : EventArgs
        {
            public int OldPrice { get; set; }
            public int NewPrice { get; set; }
        }


        /// <summary>
        /// 事件的发布者,发布事件并且在满足条件的时候,触发事件
        /// </summary>
        public class Lesson
        {
            public int Id { get; set; }
            public string Name { get; set; }

            private int _price;
            public int Price
            {
                get
                {
                    return this._price;
                }
                set
                {
                    if (value > this._price)
                    {
                        this.IncreaseHandler?.Invoke(this,
                            new XEventArgs()
                            {
                                OldPrice = this._price,
                                NewPrice = value
                            });
                        this._price = value;
                    }
                }
            }

            /// <summary>
            /// 打折事件
            /// 
            /// </summary>
            public event EventHandler IncreaseHandler;
        }
    }

  

 

posted @ 2021-01-04 15:08  YZM_Leo  阅读(108)  评论(0)    收藏  举报