事件转发

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 = ; i < ; i++)
{
MyUI el = new MyUI(er);
} long x = System.DateTime.Now.Ticks;
for (int i = ; i < ; i++)
{
int a = ;
int b = ;
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# 事件路由器的更多相关文章

  1. 戏说领域驱动设计(廿七)——Saga设计模型

    上一节我们讲解了常用的事务,也提及了Saga,这是在分布式环境下被经常使用的一种处理复杂业务和分布式事务的设计模式.本章我们的主要目标是编写一个简单版本的Saga处理器,不同于Seata框架中那种可独 ...

  2. JNI详解---从不懂到理解

    转载:https://blog.csdn.net/hui12581/article/details/44832651 Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 C ...

  3. 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备

    安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...

  4. openwrt-智能路由器hack技术(1)---"DNS劫持"

    openwrt-智能路由器hack技术(1)---"DNS劫持" 1   导读 PS:之前写的一个文章,现在发现结构内容排版不是太好,导致阅读体验太差,影响传播和SEO,所以现在整 ...

  5. 自己动手写客户端UI库——事件机制(设计思路大放送)

    在上一篇文章中我们创建了一个Button控件,并把这个控件显示在界面上, 在这一篇文章中,我们将为这个控件增加一个事件和一个方法 一:怎么绑定事件的问题 在Winform中,我们对一个按钮绑定事件的方 ...

  6. zendframework 事件管理(二)

    首先需要明确的几个问题: Q1.什么是事件? A:事件就是一个有名字的行为.当这个行为发生的时候,称这个事件被触发. Q2.监听器又是什么? A:监听器决定了事件的逻辑表达,由事件触发.监听器和事件往 ...

  7. Zend-MVC事件

    Zend\Mvc\MvcEvent继承自Zend\EventManager\Event,在Zend\Mvc\Application::bootstrap()执行时触发.如果你的控制器实现了Zend\M ...

  8. 2017年开年的第一次比较大的安全事件: MongoDB “赎金事件”,如何看待互联网安全问题

    今天上午(2017年1月7日),我的微信群中同时出现了两个MongoDB被黑掉要赎金的情况,于是在调查过程中,发现了这个事件.这个事件应该是2017年开年的第一次比较大的安全事件吧,发现国内居然没有什 ...

  9. 读取cc2530节点的设备类型、协调器、路由器、终端。

    建立网络.加入网络流程分析 协调器节点:在1-10  实验8 网络通信实验2 组播通信中 while(MSGpkt) { switch(MSGpkt->hdr.event) { case ZDO ...

随机推荐

  1. Elastic-Job-一个分布式调度解决方案

    注:Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成.Elastic-Job-Lite定位为轻量级无中心化 ...

  2. Angular-搜索框及价格上下限

    Angular-搜索框及价格上下限 闲来无事,写一个简单的angular的搜索框. 1.要求: 利用 AngularJS 框架实现手机产品搜索功能,题目要求: 1)自行查找素材,按照原有数据格式将手机 ...

  3. python实现简单函数发生器

    最近学校又抽风把我自动化系的苦逼童鞋留下做课设,简直无聊到爆的-->用VB实现函数发生器,(语言不限制) 大伙不知从哪搞来的MATLAB版本,于是几十个人就在这基础上修修改改蒙混过关了,可我实在 ...

  4. HDU 1000 A + B Problem(指针版)

    A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

    A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...

  6. 2017ccpc哈尔滨区域赛H

    n堆石子 每次只能拿一个石子从一堆移到另一堆  知道所有的堆的石子数目都能整除x(x>1) 问最小移动次数 枚举x的可能取值  即a[i]和的素因子即可  合因子的区间变化会比较大   然后求余 ...

  7. Android按下home键后重新打开app进入主activity的问题

    问题阐述: 当我们写一款App的时候,势必会有这种情况:用户已经进行了多级的操作,现返回栈中已存在多个activity,那么这个时候我们想回到最初的activity难道要一层层的返回吗,对用户来说 无 ...

  8. dede:list及dede:arclist 按权重排序的方法

    有时我们需要做文章排名,比如指定第一名到第三名在前面,这样就用到这个权重排序方法.稍改下就可以完美支持.. dede:list 的方法 1 找到"根目录\include\arc.listvi ...

  9. [SinGuLaRiTy] NOIP互测模拟赛

    [SinGuLaRiTy-1045] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 源文件名 输入输出文件 时间限制 内存限制 淘气的cch ...

  10. Centos 6.9安装配置MongoDB

    注意:centos6上就不要装mongo3了,容易出错. 1. 下载 curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2 ...