C#:错误 CS0123:"新连接处理程序"没有重载与委托<NewConnectionEventArgs>"操作"匹配



我有这个代码:

namespace Hazel
{
public struct NewConnectionEventArgs
{

public readonly MessageReader HandshakeData;

public readonly Connection Connection;
public NewConnectionEventArgs(MessageReader handshakeData, Connection connection)
{
this.HandshakeData = handshakeData;
this.Connection = connection;
}
}
}


public class UdpConnectionListener : NetworkConnectionListener
{
....
}
namespace Hazel
{

public abstract class ConnectionListener : IDisposable
{
public event Action<NewConnectionEventArgs> NewConnection;           
public abstract void Start();
}
}

我试着用来称呼它

static void Main(string[] args)
{
UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296));
listener.NewConnection += NewConnectionHandler;
}
private static void NewConnectionHandler(object sender, NewConnectionEventArgs args)
{
Console.WriteLine("New connection from " + args.Connection.EndPoint.ToString());
}

但我一直得到:

error CS0123: No overload for 'NewConnectionHandler' matches delegate 'Action<NewConnectionEventArgs>'
private static void NewConnectionHandler(object sender, NewConnectionEventArgs args)

应更改为

private static void NewConnectionHandler(NewConnectionEventArgs args)

事件类型为Action no object sender。

您必须了解什么是委托

委托类似于C和C++中的函数指针,只是它可以包含多个函数。它还可以返回一个值,如果有多个函数,则最后一个函数决定返回值。

委托是一种类型,就像类、结构和枚举一样。

示例:

public delegate void Foo(int i); // Can be outside of class
public class C
{
public static void F(int i)
{
}
}
Foo foo = null;
foo += C.F; // Add C.F to foo
foo += C.F; // Add C.F to foo, second time
foo += delegate (int i) {}; // Add an anonymous function to foo
foo += (i) => {}; // Add an anonymous function to foo
foo(); // Call all functions in foo()

如果我们想创建一个委托并立即为其分配一个函数,我们可以节省时间:

MyDelegate delegate = new MyDelegate(MyFunction);
// Is the same as
MyDelegate delegate = MyFunction;

System.ActionSystem.Func只是自.NET 2以来在.NET中预定义的一些通用委托的集合,以帮助我们。它们的定义如下:

public delegate void Action();
public delegate void Action<T1>(T1 t1);
public delegate void Action<T1, T2>(T1 t1, T2 t2);
// ...and so until T17
public delegate TResult Func<TResult>();
public delegate TResult Action<T1, TResult>(T1 t1);
public delegate TResult Action<T1, T2, TResult>(T1 t1, T2 t2);
// ...and so until T17

事件只是一个委托,有一个重要区别:您可以添加和删除事件中的方法,但可以重新分配给所有方法。为什么?举前面的例子:

public delegate void Foo(int i); // Can be outside of class
public class C
{
public static void F(int i)
{
}
}
Foo foo = null;
foo += C.F;
foo += C.F;
foo += delegate (int i) {};
foo = (i) => {}; // Oops, `=` instead of `+=`
foo(); // Call all functions in foo() - but they're just the last, is it overwritten the previous!

这通常很有用,但可能会导致事件出错。使用C#事件,我们可以使其成为编译错误:

public delegate void Foo(int i); // Can be outside of class
public class C
{
public static event Foo E; // Cannot be outside class - events are not types, but instance of delegates
public static void F(int i)
{
}
}
C.E += C.F;
C.E += C.F;
C.E += delegate (int i) {};
C.E = (i) => {}; // Compile error!
foo(); // Call all functions in foo();

System.EventHandlerSystem.EventHandler<TEventArgs>是活动的有益代表:

public delegate void EventHandler(object sender, EventArgs e);
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);

这是.NET中事件的常见模式(发送发送方和System.EventArgs的子类(,尽管没有人强制这样做。

从上面的解释中,您可以理解您的代码出了什么问题。您的代码用System.Action<T1>定义了一个事件,那么您就没有sender:

public event Action<NewConnectionEventArgs> NewConnection; 

你真正想要的是System.EventHandler<TEventArgs>:

public event EventHandler<NewConnectionEventArgs> NewConnection; 

相关内容

最新更新