设为首页 加入收藏

TOP

C#之事件(一)
2019-09-17 18:46:14 】 浏览:44
Tags:事件

事件:定义了事件成员的类允许通知其他其他对象发生了特定的事情。具体的说,定义了事件成员的类能提供以下功能

1.方法能登记它对事件的关注

2.方法能注销它对事件的关注

3.事件发生时,登记了的方法将收到通知

类型之所以能提供事件通知功能,是因为类型维护了一个已登记方法的列表。事件发生后,类型将通知列表中所有已登记的方法。

事件是以委托为基础。委托是调用回调方法的一种类型安全的方式。对象凭借回调方法接收他们订阅的通知。

 

假如有一下场景:要设计一个电子邮件程序。当有新的邮件的到达时,用户希望做些一别的操作,例如转发给其他人或其他想实现的功能。事件在其中起到的就是一个通知的作用,告诉其他对象有新的邮件到达了,可以做XXX事情了。

下面使用事件实现这个功能

1.定义一个附加信息类,用来通知接收者发生了什么。

 /// <summary>
    /// 事件附加消息
    /// </summary>
    public class NewMailEventArgs:EventArgs{
        private readonly string m_from,m_to,m_subject;
        public NewMailEventArgs(string from,string to,string subject){
            m_from=from;
            m_to=to;
            m_subject=subject;
        }
        // 发件人
        public string From { get{return m_from;} }
        // 接收人
        public string To { get{return m_to;} }
        // 主题
        public string Subject{get{return m_subject;}}
    }

附加信息类继承了EventArgs,这个基类只定义了一个空的信息,在没有附加信息时可直接使用EventArgs.Empty。EventArgs类的源代码

namespace System
{
    //
    // Summary:
    //     Represents the base class for classes that contain event data, and provides a
    //     value to use for events that do not include event data.
    public class EventArgs
    {
        //
        // Summary:
        //     Provides a value to use with events that do not have event data.
        public static readonly EventArgs Empty;

        //
        // Summary:
        //     Initializes a new instance of the System.EventArgs class.
        public EventArgs();
    }
}

 

2.定义事件成员

事件成员使用C#关键字event定义。每个事件成员都要指定以下内容:可访问标识符public(因为只有publi才能使其他对象访问),委托类型以及名称。

 public class MailManager{
        // 定义事件成员
        public event EventHandler<NewMailEventArgs> NewMail;
       
    }

它的类型是EventHandler<NewMailEventArgs> 这意味着事件通知的所有接收者都必须有一个和这个类型匹配的回调方法。System.EventHandler的委托定义类型如下:

namespace System
{
    //
    // Summary:
    //     Represents the method that will handle an event when the event provides data.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     An object that contains the event data.
    //
    // Type parameters:
    //   TEventArgs:
    //     The type of the event data generated by the event.
    public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
}

所以接收者必须提供的方法必须是一下形式:

void MethodName(Object sender,NewMailEventArgs e);

3. 定义负责引发事件的方法来通知事件的登记对象

    public class MailManager{
        // 定义事件成员
        public event EventHandler<NewMailEventArgs> NewMail;
        // 定义负责引发事件的方法来通知已登记的对象
        protected virtual void OnNewMail(NewMailEventArgs e){
            // 将字段复制到一个临时变量,避免多线程情况中这个成员被移除
            EventHandler<NewMailEventArgs> temp=Volatile.Read(ref NewMail);
            if(temp!=null) temp(this,e);
        }

        // 接受附加信息并调用引发事件的方法来通知所有登记的对象
        public void SimulateNewMail(string from,string to,string subject){
            NewMailEventArgs e=new NewMailEventArgs(from,to,subject);
            OnNewMail(e);
        }
    }

4. 定义事件接收者

   public class Fax{
        public Fax(MailManager mm){
            // 构造委托实例,向事件登记回调方法
            mm.NewMail+=FaxMsg;
        }
        /// <summary>
        /// 回调方法
        /// </summary>
        /// <param name="sender">表示MailManager对象,便于将信息传递给他</param>
        /// <param name="e">表示MailManager对象想传给我们的附加信息</param>
        private void FaxMsg(object sender,NewMailEventArgs e){
            Console.WriteLine("msg:{0},{1},{2}",e.From,e.To,e.Subject);
        }

        /// <summary>
        /// 注销对事件的登记
        /// </summary>
        /// <param name="mm"></param>
        public void Unregister(
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【译】.NET Core 3.0 中的新变化 下一篇dotnet core瘦身发布

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目