C#自界說事宜監聽完成辦法。本站提示廣大學習愛好者:(C#自界說事宜監聽完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#自界說事宜監聽完成辦法正文
本文實例講述了C#自界說事宜監聽完成辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
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>
/// 事宜監聽
/// </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>
/// 挪用類
/// </summary>
public class Run
{
static void Main(string[] args)
{
CustomEvent cem = new CustomEvent();
cem.DoRun();
Console.ReadLine();
}
}
}
願望本文所述對年夜家的C#法式設計有所贊助。