C# 提供了一个Action委托,真的超级方便
下面是对应的案例可供参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Action委托与Action事件
{
class Program
{
static void Main(string[] args)
{
ActionTest a = new ActionTest();
a.actionHandler = new Action<int, int, int, int, string, IPEndPoint>(a.ReceiveEventHandler);
a.actionEvent += a.actionHandler;
a.Run();
Console.ReadKey();
}
}
class ActionTest{
public Action<int, int, int, int, string, IPEndPoint> actionHandler;
public event Action<int, int, int, int, string, IPEndPoint> actionEvent;
public void ReceiveEventHandler(int msg, int msg1, int msg2, int msg3, string errorMsg, IPEndPoint remote)
{
Console.WriteLine("msg: " + msg);
Console.WriteLine("msg1: " + msg1);
Console.WriteLine("msg2: " + msg2);
Console.WriteLine("msg3: " + msg3);
Console.WriteLine("errorMsg: " + errorMsg);
Console.WriteLine("remote: " + remote.Address.ToString());
}
public void Run()
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 6000);
actionEvent(1,2,3,4,"hello",sender);
}
}
}
打印:
msg: 1
msg1: 2
msg2: 3
msg3: 4
errorMsg: hello
remote: 0.0.0.0