当前位置: 移动技术网 > IT编程>开发语言>c# > 详解C#之事件

详解C#之事件

2020年06月23日  | 移动技术网IT编程  | 我要评论
事件:定义了事件成员的类允许通知其他其他对象发生了特定的事情。具体的说,定义了事件成员的类能提供以下功能1.方法能登记它对事件的关注2.方法能注销它对事件的关注3.事件发生时,登记了的方法将收到通知类

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

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(mailmanager mm){
      mm.newmail-=faxmsg;
    }
  }

对象不在接收事件通知时应注销对事件的关注。因为对象只要向事件等急了它的一个方法,便不能被垃圾回收。

5. 程序初始化时应首先构造mailmanager对象,将指向它的变量传递给fax。在fax构造器中添加对事件的关注。最后调用mailmanager对象的事件通知方法

static void main(string[] args)
    {
      mailmanager mm=new mailmanager();
      fax f=new fax(mm);
      mm.simulatenewmail("a","b","hello world!");
      console.readkey();
    }

控制台输出结果:以调用回调方法。

以上就是详解c#之事件的详细内容,更多关于c#之事件的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网