閱讀934 返回首頁    go 阿裏雲 go 技術社區[雲棲]


多個委托方法的順序執行

using System;
namespace 委托和事件
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Action action = One;
            action += Two;
            action += Three;
            Delegate[] delegates = action.GetInvocationList(); //返回委托掛接的方法,通過他可以控製委托方法執行順序
            foreach (Action delegateAction in delegates)
            {
                try
                {
                    delegateAction();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Console.Read();
        }
        private static void One()
        {
            Console.WriteLine("調用:方法一");
            throw new Exception("Err in one");
        }
        private static void Two()
        {
            Console.WriteLine("調用:方法二");
        }
        private static void Three()
        {
            Console.WriteLine("調用:方法三");
        }
    }
}

最後更新:2017-04-03 18:51:45

  上一篇:go 再次寫給我們這些浮躁的程序員
  下一篇:go 在Ubuntu下安裝Gnome 3