C# 为一个方法添加事件的方法 Event delegate

时间:2023-01-09 00:19:54

using System;

public delegate void LogEventHandler(object source, LogEventArgs args);

public class EventDemo
{
    public event LogEventHandler logEvent;

    public void Do()
    {
        logEvent(this,new LogEventArgs());

        Console.WriteLine("Ok.");
    }
}
public class LogEventArgs : EventArgs
{
    public LogEventArgs()
    {}

}
public class App
{
    public static void Main()
    {
     EventDemo eventDemo =new EventDemo();
     eventDemo.logEvent += new LogEventHandler(WriteLog);

     eventDemo.Do();

    }
    public static void WriteLog(object sender,LogEventArgs le)
    {
     Console.WriteLine("In Event :Write Done. ");
    }

}

相关文章