首页 > 代码库 > c# 事件路由器

c# 事件路由器

事件转发

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace RouteSystem{    #region action define    public interface IGAction    {        void Execute();        void Execute<T1>(T1 arg1);        void Execute<T1, T2>(T1 arg1, T2 arg2);        void Execute<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);        void Execute<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);        void Execute<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);        void Execute<T1, T2, T3, T4, T5, T6>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);        void Execute<T1, T2, T3, T4, T5, T6, T7>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);    }    public class GAction : IGAction    {        Action action = null;        public GAction(Action action_) { action = action_; }        public void Execute()        { action(); }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5,U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1> : IGAction    {        Action<T1> action = null;        public GAction(Action<T1> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1)        { action((T1)(object)(arg1)); }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2> : IGAction    {        Action<T1, T2> action = null;        public GAction(Action<T1, T2> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2)        { action((T1)(object)(arg1), (T2)(object)arg2); }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2, T3> : IGAction    {        Action<T1, T2, T3> action = null;        public GAction(Action<T1, T2, T3> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3)        { action((T1)(object)(arg1), (T2)(object)arg2, (T3)(object)arg3); }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2, T3, T4> : IGAction    {        Action<T1, T2, T3, T4> action = null;        public GAction(Action<T1, T2, T3, T4> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4)        { action((T1)(object)(arg1), (T2)(object)arg2, (T3)(object)arg3, (T4)(object)arg4); }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2, T3, T4, T5> : IGAction    {        Action<T1, T2, T3, T4, T5> action = null;        public GAction(Action<T1, T2, T3, T4, T5> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5)        { action((T1)(object)(arg1), (T2)(object)arg2, (T3)(object)arg3, (T4)(object)arg4, (T5)(object)arg5); }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2, T3, T4, T5, T6> : IGAction    {        Action<T1, T2, T3, T4, T5, T6> action = null;        public GAction(Action<T1, T2, T3, T4, T5, T6> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6)        { action((T1)(object)(arg1), (T2)(object)arg2, (T3)(object)arg3, (T4)(object)arg4, (T5)(object)arg5, (T6)(object)arg6); }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7) { }    }    public class GAction<T1, T2, T3, T4, T5, T6, T7> : IGAction    {        Action<T1, T2, T3, T4, T5, T6, T7> action = null;        public GAction(Action<T1, T2, T3, T4, T5, T6, T7> action_) { action = action_; }        public void Execute() { }        public void Execute<U1>(U1 arg1) { }        public void Execute<U1, U2>(U1 arg1, U2 arg2) { }        public void Execute<U1, U2, U3>(U1 arg1, U2 arg2, U3 arg3) { }        public void Execute<U1, U2, U3, U4>(U1 arg1, U2 arg2, U3 arg3, U4 arg4) { }        public void Execute<U1, U2, U3, U4, U5>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5) { }        public void Execute<U1, U2, U3, U4, U5, U6>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6) { }        public void Execute<U1, U2, U3, U4, U5, U6, U7>(U1 arg1, U2 arg2, U3 arg3, U4 arg4, U5 arg5, U6 arg6, U7 arg7)        { action((T1)(object)(arg1), (T2)(object)arg2, (T3)(object)arg3, (T4)(object)arg4, (T5)(object)arg5, (T6)(object)arg6, (T7)(object)arg7); }    }    #endregion    public class EventListener    {        List<IGAction> reactions = null;        EventRouter router = null;        public EventListener(EventRouter er) { router = er; }        public void DetachEvents()        {            if (reactions == null || router == null)                return;                        foreach(IGAction reaction in reactions)            { router.RemoveReaction(reaction); }        }                #region //listen event        protected void ListenEvent(string sevent, Action cb) {            IGAction igaction = new GAction(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1>(string sevent, Action<T1> cb) {            IGAction igaction = new GAction<T1>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2>(string sevent, Action<T1, T2> cb) {            IGAction igaction = new GAction<T1, T2>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2, T3>(string sevent, Action<T1, T2, T3> cb) {            IGAction igaction = new GAction<T1, T2, T3>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2, T3, T4>(string sevent, Action<T1, T2, T3, T4> cb) {            IGAction igaction = new GAction<T1, T2, T3, T4>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2, T3, T4, T5>(string sevent, Action<T1, T2, T3, T4, T5> cb) {            IGAction igaction = new GAction<T1, T2, T3, T4, T5>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2, T3, T4, T5, T6>(string sevent, Action<T1, T2, T3, T4, T5, T6> cb) {            IGAction igaction = new GAction<T1, T2, T3, T4, T5, T6>(cb);            AddReaction(sevent, igaction);        }        protected void ListenEvent<T1, T2, T3, T4, T5, T6, T7>(string sevent, Action<T1, T2, T3, T4, T5, T6, T7> cb) {            IGAction igaction = new GAction<T1, T2, T3, T4, T5, T6, T7>(cb);            AddReaction(sevent, igaction);        }        #endregion        private void AddReaction(string sevent, IGAction reaction_)        {            if (router == null) return;            router.AddReaction(sevent, reaction_);            if (reactions == null) reactions = new List<IGAction>();            reactions.Add(reaction_);        }    }    public class EventRouter    {        Dictionary<string, List<IGAction>> containers = new Dictionary<string, List<IGAction>>();        public void AddReaction(string sevent, IGAction reaction)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null)            {                reactionlist = new List<IGAction>();                containers.Add(sevent, reactionlist);            }            IGAction old = reactionlist.Find(x => x == reaction);            if(old == null) reactionlist.Add(reaction);        }        public void RemoveReaction(IGAction reaction)        {            foreach(KeyValuePair<string, List<IGAction>> kv in containers)            {                IGAction old = kv.Value.Find(x => x == reaction);                if (old != null)                {                    kv.Value.Remove(old);                    break;                }            }        }        public List<IGAction> GetReactionList(string sevent)        {            List<IGAction> reactionlist = null;            containers.TryGetValue(sevent, out reactionlist);            return reactionlist;        }        #region // send event        public void SendEvent(string sevent)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute();            }        }        public void SendEvent<T1>(string sevent, T1 arg1)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1);            }        }        public void SendEvent<T1, T2>(string sevent, T1 arg1, T2 arg2)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2);            }        }        public void SendEvent<T1, T2, T3>(string sevent, T1 arg1, T2 arg2, T3 arg3)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2, arg3);            }        }        public void SendEvent<T1, T2, T3, T4>(string sevent, T1 arg1, T2 arg2, T3 arg3, T4 arg4)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2, arg3, arg4);            }        }        public void SendEvent<T1, T2, T3, T4, T5>(string sevent, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2, arg3, arg4, arg5);            }        }        public void SendEvent<T1, T2, T3, T4, T5, T6>(string sevent, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2, arg3, arg4, arg5, arg6);            }        }        public void SendEvent<T1, T2, T3, T4, T5, T6, T7>(string sevent, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)        {            List<IGAction> reactionlist = GetReactionList(sevent);            if (reactionlist == null) return;            foreach (IGAction el in reactionlist)            {                el.Execute(arg1, arg2, arg3, arg4, arg5, arg6, arg7);            }        }        #endregion    }}

 使用

public class MyUI : RouteSystem.EventListener    {        void nothing() { /*Console.WriteLine("nothing");*/ }        void sayhi(string h) { /*Console.WriteLine("sayhi");*/ }        void run(int a) { /*Console.WriteLine("run");*/ }        void jump(int a, int r) { /*Console.WriteLine("jump");*/ }        void mix(int a, int b, char s, float c, float d) { /*Console.WriteLine("mix");*/ }        public MyUI(RouteSystem.EventRouter er):base(er)        {            ListenEvent("nothing", nothing);            ListenEvent<string>("sayhi", sayhi);            ListenEvent<int>("run", run);            ListenEvent<int, int>("jump", jump);            ListenEvent<int, int, char, float, float>("mix", mix);        }    }    class Program    {                static void Main(string[] args)        {            RouteSystem.EventRouter er = new RouteSystem.EventRouter();            for (int i = 0; i < 100; i++)            {                MyUI el = new MyUI(er);            }            long x = System.DateTime.Now.Ticks;            for (int i = 0; i < 1000000; i++)            {                int a = 1;                int b = 2;                char c = a;                float d = 0f;                float e = 1f;                er.SendEvent("run", a);            }            x = System.DateTime.Now.Ticks - x;            Console.WriteLine("cost " + x.ToString());        }            }

 

c# 事件路由器