.net事件实例代码

/*
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);
        }
    }
}
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值