class MailManager
![]()
{
//在MailManager内部定义MailMsgEventArgs类型
public class MailMsgEventArgs : EventArgs
![]()
{
//传递给事件接受者的类型定义信息
public MailMsgEventArgs(string from, String to, string subject, string body)
![]()
{
this.form = form;
this.to = to;
this.subject = subject;
this.body = body;
}
![]()
public readonly string form, to, subject, body;
}
![]()
//下面的委托类型定义了接受者必须实现的回调方法原型
public delegate void MailMsgEventHandler(object sender, MailMsgEventArgs args);
![]()
//事件成员
public event MailMsgEventHandler MailMsg;
![]()
//下面的受保护虚方法负责通知时间的登记对象
protected virtual void OnMailMsg(MailMsgEventArgs e)
![]()
{
if (MailMsg != null)
![]()
{
MailMsg(this, e);
}
}
![]()
//转化为期望的事件,方法调用
public void SimulateArrivingMsg(string from, string to, string subject, string body)
![]()
{
//构造一个对象保存希望传递给通知接受者的信息
MailMsgEventArgs e = new MailMsgEventArgs(from, to, subject, body);
//调用虚方法通知对象事件已发生
//如果派生类型没有重写该虚方法
//对象将通知所有等级的事件侦听者
OnMailMsg(e);
}
}
posted @
2007-10-24 21:15
子午
阅读(
212)
评论()
收藏
举报