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 = ; 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# 事件路由器的更多相关文章
- 戏说领域驱动设计(廿七)——Saga设计模型
上一节我们讲解了常用的事务,也提及了Saga,这是在分布式环境下被经常使用的一种处理复杂业务和分布式事务的设计模式.本章我们的主要目标是编写一个简单版本的Saga处理器,不同于Seata框架中那种可独 ...
- JNI详解---从不懂到理解
转载:https://blog.csdn.net/hui12581/article/details/44832651 Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 C ...
- 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备
安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...
- openwrt-智能路由器hack技术(1)---"DNS劫持"
openwrt-智能路由器hack技术(1)---"DNS劫持" 1 导读 PS:之前写的一个文章,现在发现结构内容排版不是太好,导致阅读体验太差,影响传播和SEO,所以现在整 ...
- 自己动手写客户端UI库——事件机制(设计思路大放送)
在上一篇文章中我们创建了一个Button控件,并把这个控件显示在界面上, 在这一篇文章中,我们将为这个控件增加一个事件和一个方法 一:怎么绑定事件的问题 在Winform中,我们对一个按钮绑定事件的方 ...
- zendframework 事件管理(二)
首先需要明确的几个问题: Q1.什么是事件? A:事件就是一个有名字的行为.当这个行为发生的时候,称这个事件被触发. Q2.监听器又是什么? A:监听器决定了事件的逻辑表达,由事件触发.监听器和事件往 ...
- Zend-MVC事件
Zend\Mvc\MvcEvent继承自Zend\EventManager\Event,在Zend\Mvc\Application::bootstrap()执行时触发.如果你的控制器实现了Zend\M ...
- 2017年开年的第一次比较大的安全事件: MongoDB “赎金事件”,如何看待互联网安全问题
今天上午(2017年1月7日),我的微信群中同时出现了两个MongoDB被黑掉要赎金的情况,于是在调查过程中,发现了这个事件.这个事件应该是2017年开年的第一次比较大的安全事件吧,发现国内居然没有什 ...
- 读取cc2530节点的设备类型、协调器、路由器、终端。
建立网络.加入网络流程分析 协调器节点:在1-10 实验8 网络通信实验2 组播通信中 while(MSGpkt) { switch(MSGpkt->hdr.event) { case ZDO ...
随机推荐
- css 两端对齐的多种实现方式
http://www.cnblogs.com/PeunZhang/p/3289493.html
- python 虚拟环境--virtualenv
virtualenv 是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包. 安装方式一: pip install v ...
- C#学习笔记-状态模式
题目1:通过代码描述每一天的不同时间段的工作效率 分析: 首先确定,工作状态指正在工作,或者以及下班这些情况,而这些情况所受影响的因素包括:当前时间以及任务是否已经完成.所以在Work中需要两个属性: ...
- Django基础学习笔记
Django开发流程 创建一个django项目:命令:django-admin startproject 项目名 进入到项目并创建一个应用:命令:python manage.py startapp 应 ...
- 详解css3弹性盒模型(Flexbox)
目前没有浏览器支持 box-flex 属性. Firefox 支持替代的 -moz-box-flex 属性. Safari.Opera 以及 Chrome 支持替代的 -webkit-box-flex ...
- js面向对象学习笔记(一):创建空对象,理解this指向
var obj = new Object();//创建一个空对象 obj.name = '小王';//属性 obj.sayName = function () { //对象方法 对象最重要的是this ...
- Codefoces 723A The New Year: Meeting Friends
A. The New Year: Meeting Friends time limit per test:1 second memory limit per test:256 megabytes in ...
- CDOJ 1330 柱爷与远古法阵【高斯消元,卡精度】
柱爷与远古法阵 Time Limit: 125/125MS (Java/Others) Memory Limit: 240000/240000KB (Java/Others) Submit S ...
- [bzoj1223] [HNOI2002]Kathy函数
首先由题解可得TAT,f(i)=i当且仅当i在二进制下为回文串. 那么问题就变成了1~n中有多少个二进制下的回文串. 把m转成2进制后就是正常的统计了= =. f[i]表示二进制下,有多少个i位的回文 ...
- CSS3技巧巧妙使用:not(:last-of-type)简化你的css代码
终于找到了一个好方法,使用:not(:last-of-type)简单方便,再也不要麻烦的单独使用:last-of-type了,不错! 应用场景:平时我们的列表一般都会有分割线,但是最后一个列表没有分割 ...