阅读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