//寄送報紙
        public void SendNewspaper(Newspaper_MulticastDelegete newspaper)
        {
            newspaper.PublisherName = this.Name;
            //使用委託
            if (_Subscribers != null)
            {
                //_Subscribers(newspaper);

                //如果在多播委託時產生例外:
                //將_Subscribers中的調用委託函數列表迭代出來
                foreach (Subscribers hander in _Subscribers.GetInvocationList())
                {
                    try
                    {
                        //每一個元素都是Subscribers類型函數,調用hander(參數);
                        hander(newspaper);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Example #2
0
            public void SendNewsPaper(NewsPaper newsPaper)
            {
                //newsPaper.publisherName = this.name;
                //这是一股脑的处理,没法处理异常
                //使用委托
                //subscribers?.Invoke(newsPaper);//Event?.Invoke() 若event不为null,则baiinvoke,这是C#6的新语法du。zhi   ?.称为空值传dao播运算符。

                //异常处理方法
                if (Subscribers != null)
                {
                    //转为list一个一个处理
                    foreach (EventHandler <PublisherArgs> subscriber in Subscribers.GetInvocationList())
                    {
                        try
                        {
                            subscriber(this, new PublisherArgs(newsPaper));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }