C#事件——对委托的封装
2017-03-13 09:17
435 查看
C#中,正如属性是对成员变量的封装,事件是对委托的封装。
完整的事件流程:
class Program
{
static void Main(string[] args)
{
EventSender sender = new EventSender();
EventRevicer revicer = new EventRevicer();
sender.myEvent += revicer.Action;
//invoke the event
sender.Active(new MyEventArgs("C# Event"));
}
}
class MyEventArgs:EventArgs
{
public MyEventArgs(string name)
{
this.name = name;
}
public string name { get; set; }
}
delegate void MyEventHandler(EventSender sender,MyEventArgs e);
class EventSender
{
private MyEventHandler myEventHandler;
public event MyEventHandler myEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}
public void Active(MyEventArgs e)
{
Console.WriteLine("I send the notification");
myEventHandler.Invoke(this, e);
}
}
class EventRevicer
{
internal void Action(EventSender sender, MyEventArgs e)
{
Console.WriteLine("I get the notification!");
}
}
简写的事件流程:
class Program
{
static void Main(string[] args)
{
EventSender sender = new EventSender();
EventRevicer revicer = new EventRevicer();
sender.myEvent += revicer.Action;
//invoke the event
sender.Active(new MyEventArgs("C# Event"));
}
}
class MyEventArgs : EventArgs
{
public MyEventArgs(string name)
{
this.name = name;
}
public string name { get; set; }
}
class EventSender
{
public event EventHandler myEvent;//the event
public void Active(MyEventArgs e)
{
Console.WriteLine("I send the notification");
myEvent.Invoke(this, e);
}
}
class EventRevicer
{
internal void Action(object sender, EventArgs e)
{
//Type convert
MyEventArgs args = e as MyEventArgs;
Console.WriteLine("I get the notification! args:{0}",args.name);
}
}
注:我们平时经常用的简写流程中省略了事件封装的过程,但实际上C# 语言机制内部是有这个对委托的封装过程的。
完整的事件流程:
class Program
{
static void Main(string[] args)
{
EventSender sender = new EventSender();
EventRevicer revicer = new EventRevicer();
sender.myEvent += revicer.Action;
//invoke the event
sender.Active(new MyEventArgs("C# Event"));
}
}
class MyEventArgs:EventArgs
{
public MyEventArgs(string name)
{
this.name = name;
}
public string name { get; set; }
}
delegate void MyEventHandler(EventSender sender,MyEventArgs e);
class EventSender
{
private MyEventHandler myEventHandler;
public event MyEventHandler myEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}
public void Active(MyEventArgs e)
{
Console.WriteLine("I send the notification");
myEventHandler.Invoke(this, e);
}
}
class EventRevicer
{
internal void Action(EventSender sender, MyEventArgs e)
{
Console.WriteLine("I get the notification!");
}
}
简写的事件流程:
class Program
{
static void Main(string[] args)
{
EventSender sender = new EventSender();
EventRevicer revicer = new EventRevicer();
sender.myEvent += revicer.Action;
//invoke the event
sender.Active(new MyEventArgs("C# Event"));
}
}
class MyEventArgs : EventArgs
{
public MyEventArgs(string name)
{
this.name = name;
}
public string name { get; set; }
}
class EventSender
{
public event EventHandler myEvent;//the event
public void Active(MyEventArgs e)
{
Console.WriteLine("I send the notification");
myEvent.Invoke(this, e);
}
}
class EventRevicer
{
internal void Action(object sender, EventArgs e)
{
//Type convert
MyEventArgs args = e as MyEventArgs;
Console.WriteLine("I get the notification! args:{0}",args.name);
}
}
注:我们平时经常用的简写流程中省略了事件封装的过程,但实际上C# 语言机制内部是有这个对委托的封装过程的。
相关文章推荐
- 【原创】C# 委托与事件的传递与封装案例
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- C#里的委托和事件实现Observer
- C#中的委托与事件
- C#中的委托与事件[翻译](转)
- 我对C#中事件委托的通俗看法
- C#中的组件-事件-委托
- C#里的委托和事件实现Observer
- C#的委托与Delphi中的"事件委托"
- C#中的委托与事件[翻译]
- C#中利用委托传递事件
- 深入理解C#编程中的组件-事件-委托
- 用 C++ 实现 C# 中的 委托/事件 (5-functor2)
- 深入理解C#编程中的组件-事件-委托
- C# 事件 委托 教程
- 用C++实现C#中的委托/事件(标准C++之升级版)
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- [转载]C#里的委托和事件实现Observer
- 用 C++ 实现 C# 中的 委托/事件 (4-functor1)
- C#委托及事件