欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#自定義事件監(jiān)聽實現(xiàn)方法

 更新時間:2015年08月22日 17:15:58   作者:我心依舊  
這篇文章主要介紹了C#自定義事件監(jiān)聽實現(xiàn)方法,涉及C#事件監(jiān)聽的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#自定義事件監(jiān)聽實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp
{
 /// <summary>
 /// 定義事件
 /// </summary>
 class CustomEvent
 {
  /// <summary>
  /// 定義委托
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public delegate void UserRequest(object sender, EventArgs e);
  /// <summary>
  /// 此委托類型的事件
  /// </summary>
  public event UserRequest OnUserRequest;
  public CustomEvent()
  {
   UserEventMonitor uem = new UserEventMonitor(this);
  }
  public void DoRun()
  {
   bool flag = false;
   do
   {
    Console.WriteLine();
    Console.WriteLine("請輸入:");
    string result = Console.ReadLine();
    if (result == "1")
    {
     if (OnUserRequest != null)
      OnUserRequest(this, new EventArgs());
    }
   } while (!flag);
  }
 }
 /// <summary>
 /// 事件監(jiān)聽
 /// </summary>
 class UserEventMonitor
 {
  public UserEventMonitor(CustomEvent cem)
  {
   // cem.OnUserRequest += ShowMessage;
   cem.OnUserRequest += delegate
   {
    Console.WriteLine("hello word!!");
   };
  }
  public void ShowMessage(object sender, EventArgs e)
  {
   Console.WriteLine("hello word!!");
  }
 }
 /// <summary>
 /// 調(diào)用類
 /// </summary>
 public class Run
 {
  static void Main(string[] args)
  {
   CustomEvent cem = new CustomEvent();
   cem.DoRun();
   Console.ReadLine();
  }
 }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

最新評論