委托链——Delegate.Combine()

作者:追风剑情 发布于:2016-2-19 10:48 分类:C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CombineTest
{
    public delegate void Log(string message);

    class Program
    {
        public static Log LogDelegate { get; set; }

        public static Log LogDelegate1 { get; set; }
        public static Log LogDelegate2 { get; set; }

        static void Main(string[] args)
        {
            LogDelegate1 += WriteLog1;
            LogDelegate1 += WriteLog2;

            LogDelegate2 += WriteLog3;
            LogDelegate2 += WriteLog4;

            //合并LogDelegate1与LogDelegate2
            LogDelegate = (Log)Delegate.Combine(LogDelegate1, LogDelegate2);
            LogDelegate("test");

            //移除LogDelegate1
            LogDelegate = (Log)Delegate.Remove(LogDelegate, LogDelegate1);
            LogDelegate("test1");

            Console.Read();
        }

        private static void WriteLog1(string message)
        {
            Console.WriteLine("WriteLog1: "+message);
        }

        private static void WriteLog2(string message)
        {
            Console.WriteLine("WriteLog2: " + message);
        }

        private static void WriteLog3(string message)
        {
            Console.WriteLine("WriteLog3: " + message);
        }

        private static void WriteLog4(string message)
        {
            Console.WriteLine("WriteLog4: " + message);
        }
    }
}

运行结果

111111111.png

 

标签: C#

« 摇杆 | 行为树»
Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号