C# 委托 多播模式

using System;

namespace ConsoleApp1
{

    public delegate void OneDelegateHandler();

    class A
    {
        public   OneDelegateHandler AEvent;
        public void onTrigger()
        {
            if (AEvent != null)
            {
                AEvent();
            }
        }
    }
    class B
    {
        public B(A a) { a.AEvent += B_AEvent; }

        private static void B_AEvent()
        {
            Console.WriteLine("::B");
        }
    }

    class C
    {
        public C(A a)
        {
            a.AEvent = (OneDelegateHandler)Delegate.Combine(a.AEvent, new OneDelegateHandler(this.B_AEvent));
        }

        private  void B_AEvent()
        {
            Console.WriteLine("::C");
        }


    }


    class Program
    {
        static void Main(string[] args)
        {
            A iamA = new A();
            B iamB = new B(iamA);
            C iamC = new C(iamA);

            iamA.onTrigger();

            Console.Read();
        }
    }
}

 

你可能感兴趣的:(C#知识点)