C# 事件总线

参考网上博客以及Github上面的源码,最后的代码如下:

public interface IEventBus
    {
        void Suscribe(Type tevent, Action action);

        void Suscribe<T1>(Type tevent, Action<T1> action);

        void Suscribe<T1, T2>(Type tevent, Action<T1, T2> action);

        void Suscribe<T1, T2, T3>(Type tevent, Action<T1, T2, T3> action);

        void Suscribe<T1, T2, T3, T4>(Type tevent, Action<T1, T2, T3, T4> action);

        void Suscribe<T1, T2, T3, T4, T5>(Type tevent, Action<T1, T2, T3, T4, T5> action);

        void Suscribe<T1, T2, T3, T4, T5, T6>(Type tevent, Action<T1, T2, T3, T4, T5, T6> action);

        void PostMessage(Type TEvent,params object[] args);

        void UnSuscribe(Type tevent);
    }

    public class EventBus :IEventBus
    {
        private Dictionary<Type, List<EventArgs>> dict = new Dictionary<Type, List<EventArgs>>();

        private static EventBus eventBus;
        public static EventBus MyEventBus
        {
            get
            {
                if (eventBus == null)
                {
                    eventBus = new EventBus();
                }
                return eventBus;
            }
        }

        public void PostMessage(Type type,params object[] args)
        {
            List<EventArgs> arr = dict[type];
            foreach(EventArgs eventArgs in arr)
            {
                eventArgs.ActiveMethod.Invoke(eventArgs.EventHost, args);
            }
        }

        public void Suscribe(Type tevent,Action action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1>(Type tevent, Action<T1> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1, T2>(Type tevent, Action<T1, T2> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1, T2, T3>(Type tevent, Action<T1,T2,T3> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1, T2, T3,T4>(Type tevent, Action<T1, T2, T3, T4> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1, T2, T3,T4,T5>(Type tevent, Action<T1, T2, T3, T4, T5> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void Suscribe<T1, T2, T3, T4, T5,T6>(Type tevent, Action<T1, T2, T3, T4, T5, T6> action)
        {
            if (!dict.Keys.Contains(tevent))
            {
                dict[tevent] = new List<EventArgs>();
            }
            dict[tevent].Add(new EventArgs()
            {
                EventHost = action.Target,
                ActiveMethod = action.Method
            });
        }

        public void UnSuscribe(Type tevent)
        {
            List<EventArgs> arr = dict[tevent];
            arr.Clear();
            arr = null;
        }
    }

    public class EventArgs
    {
        public object EventHost { get; set; }

        public MethodInfo ActiveMethod { get; set; }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值