/*
using System;
using System.Collections.Generic;
using System.Text;
namespace EventTest1
{
public class MailMsgEventArgs : EventArgs
{
public readonly String from, to, subject, body;
//1.传递事件接受者的类型定义信息
public MailMsgEventArgs(String from, String to,
String subject, String body)
{
this.from = from;
this.to = to;
this.subject = subject;
this.body = body;
}
}
class MailManager
{
//2.下面的委托类型定义了接受者必须实现的回调方法原型
public delegate void MailMsgEventHandler(Object sender, MailMsgEventArgs args);
//3.事件成员
public event MailMsgEventHandler MailMsg;
//4.下面的受保护虚方法负责通知事件的登记对象
protected virtual void OnMailMsg(MailMsgEventArgs e)
{
//如果有对象登记事件则通知委托链表上的所有对象
if (MailMsg != null)
MailMsg(this, e);
}
//5.下面的方法将输入转化为期望的事件,该方法在新的电子邮件消息到达时被调用
public void SimulateArrivingMsg(String from, String to,
String subject, String body)
{
//构造一个对象保存希望传递给通知接受者的信息
MailMsgEventArgs e = new MailMsgEventArgs(from, to, subject, body);
//调用虚方法通知对象事件已发生,如果派生类型没有重写该虚方法,对象将通知所有登记的事件侦听者
OnMailMsg(e);
}
}
class MailTest
{
static void Main(String[] args)
{
MailManager manager =new MailManager();
Console.WriteLine("注册事件后,可以引发该事件");
manager.MailMsg += new MailManager.MailMsgEventHandler(TestMailHander);
//引发事件显示邮件信息
manager.SimulateArrivingMsg("from李", "to宝", "subject主题", "body内容");
Console.WriteLine("注销事件后,无法再次引发该事件");
manager.MailMsg -= new MailManager.MailMsgEventHandler(TestMailHander);
//无法显示邮件信息
manager.SimulateArrivingMsg("from李", "to宝", "subject主题", "body内容");
Console.Read();
}
static protected void TestMailHander(Object o,MailMsgEventArgs e)
{
Console.WriteLine("{0}-{1}-{2}-{3}",e.from,e.to,e.subject,e.body);
}
}
}
*/
/*
using System;
using System.Collections.Generic;
using System.Text;
namespace EventTest1
{
public class MailMsgEventArgs : EventArgs
{
public readonly String from, to, subject, body;
//1.传递事件接受者的类型定义信息
public MailMsgEventArgs(String from, String to,
String subject, String body)
{
this.from = from;
this.to = to;
this.subject = subject;
this.body = body;
}
}
class MailManager
{
//2.下面的委托类型定义了接受者必须实现的回调方法原型
public delegate void MailMsgEventHandler(Object sender, MailMsgEventArgs args);
//3a.显示定义一个私有委托链表字段
private MailMsgEventHandler mailMsgEventHandlerDelegate;
//3b.显示定义事件及其访问器方法
public event MailMsgEventHandler MailMsg
{
add
{
mailMsgEventHandlerDelegate = (MailMsgEventHandler)Delegate.Combine(mailMsgEventHandlerDelegate, value);
}
remove
{
mailMsgEventHandlerDelegate = (MailMsgEventHandler)Delegate.Remove(mailMsgEventHandlerDelegate, value);
}
}
//4.下面的受保护虚方法负责通知事件的登记对象
protected virtual void OnMailMsg(MailMsgEventArgs e)
{
//如果有对象登记事件则通知委托链表上的所有对象
if (mailMsgEventHandlerDelegate != null)
mailMsgEventHandlerDelegate(this, e);
}
//5.下面的方法将输入转化为期望的事件,该方法在新的电子邮件消息到达时被调用
public void SimulateArrivingMsg(String from, String to,
String subject, String body)
{
//构造一个对象保存希望传递给通知接受者的信息
MailMsgEventArgs e = new MailMsgEventArgs(from, to, subject, body);
//调用虚方法通知对象事件已发生,如果派生类型没有重写该虚方法,对象将通知所有登记的事件侦听者
OnMailMsg(e);
}
}
class MailTest
{
static void Main(String[] args)
{
MailManager manager = new MailManager();
Console.WriteLine("注册事件后,可以引发该事件");
manager.MailMsg += new MailManager.MailMsgEventHandler(TestMailHander);
//引发事件显示邮件信息
manager.SimulateArrivingMsg("from李", "to宝", "subject主题", "body内容");
Console.WriteLine("注销事件后,无法再次引发该事件");
manager.MailMsg -= new MailManager.MailMsgEventHandler(TestMailHander);
//无法显示邮件信息
manager.SimulateArrivingMsg("from李", "to宝", "subject主题", "body内容");
Console.Read();
}
static protected void TestMailHander(Object o, MailMsgEventArgs e)
{
Console.WriteLine("{0}-{1}-{2}-{3}", e.from, e.to, e.subject, e.body);
}
}
}
*/