自己动手用委托模拟.net中的事件机制
2016-02-26 18:32
357 查看
原理:委托支持多播委托,即:可以将多个方法绑定到同一个委托变量上(.NET有十分方便的语法实现和解除多播委托:+=和-=),形成所谓的委托链。
在.net开发中,我们之所以能方便地对某个控件增加事件,就是因为.net控件巧妙地封装了委托,这个从其增加事件的代码中可以很清除地看出:
this.button1.Click += new System.EventHandler(this.button1_Click)
private void button1_Click(object sender,EventArgs e){...}
知道了原理,就可以自己模拟出事件机制,代码:
// 定义被触发者
public class Caculator
{
public class CaculateEvents : EventArgs
{
public readonly int x, y;
public CaculateEvents( int x, int y)
{
this .x = x;
this .y = y;
}
}
public delegate void CaculateEventHander( object sender, CaculateEvents e);
public event CaculateEventHander myhandler;
protected virtual void onCaculate(CaculateEvents e)
{
if (myhandler != null )
{
myhandler( this , e);
}
}
public void Caculate( int x, int y)
{
CaculateEvents e = new CaculateEvents(x, y);
onCaculate(e);
}
}
// 定义呼叫者
class caller
{
public void Add( object sender, Caculator.CaculateEvents e)
{
Console.WriteLine(e.x + e.y);
}
public void Sub( object sender, Caculator.CaculateEvents e)
{
Console.WriteLine(e.x - e.y);
}
}
// 模拟事件
class Program
{
static void Main( string [] args)
{
caller mycaler = new caller();
Caculator caculator = new Caculator();
caculator.myhandler += mycaler.Add;
caculator.myhandler += mycaler.Sub;
caculator.Caculate( 100 , 200 );
Console.ReadLine();
}
}
在.net开发中,我们之所以能方便地对某个控件增加事件,就是因为.net控件巧妙地封装了委托,这个从其增加事件的代码中可以很清除地看出:
this.button1.Click += new System.EventHandler(this.button1_Click)
private void button1_Click(object sender,EventArgs e){...}
知道了原理,就可以自己模拟出事件机制,代码:
// 定义被触发者
public class Caculator
{
public class CaculateEvents : EventArgs
{
public readonly int x, y;
public CaculateEvents( int x, int y)
{
this .x = x;
this .y = y;
}
}
public delegate void CaculateEventHander( object sender, CaculateEvents e);
public event CaculateEventHander myhandler;
protected virtual void onCaculate(CaculateEvents e)
{
if (myhandler != null )
{
myhandler( this , e);
}
}
public void Caculate( int x, int y)
{
CaculateEvents e = new CaculateEvents(x, y);
onCaculate(e);
}
}
// 定义呼叫者
class caller
{
public void Add( object sender, Caculator.CaculateEvents e)
{
Console.WriteLine(e.x + e.y);
}
public void Sub( object sender, Caculator.CaculateEvents e)
{
Console.WriteLine(e.x - e.y);
}
}
// 模拟事件
class Program
{
static void Main( string [] args)
{
caller mycaler = new caller();
Caculator caculator = new Caculator();
caculator.myhandler += mycaler.Add;
caculator.myhandler += mycaler.Sub;
caculator.Caculate( 100 , 200 );
Console.ReadLine();
}
}
相关文章推荐
- 如何在 Linux/Windows/MacOS 上使用 .NET 进行开发
- 如何在 Linux 中安装微软的 .NET Core SDK
- C#.NET获取拨号连接的宽带连接方法
- C#.Net ArrayList的使用方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- .net(c#)中的new关键字详细介绍
- 由vbs sort引发.NET Framework之间的关系说明
- C#难点逐个击破(6):C#数据类型与.net framework数据类型
- .NET中的async和await关键字使用及Task异步调用实例
- .Net中的json操作类用法分析
- .net实现序列化与反序列化实例解析
- .NET中的Timer类型用法详解
- 关于.net(C#)中的跨进程访问的问题
- .NET实现父窗体关闭而不影响子窗体的方法
- 基于.Net中的数字与日期格式化规则助记词的使用详解
- .NET微信公众号开发之公众号消息处理
- .Net下的签名与混淆图文分析
- .NET 扩展实现代码
- .net实现网站用户登录认证
- .NET实现定时发送邮件代码(两种方式)