我正在尝试使用Coded for .NET 4编译库并重新编译它以使用.NET 3.5 Client。该库位于https://github.com/cshivers/IrcClient-csharp/tree/master/IrcClient-csharp
这是我的程序中的代码块,在外部库中调用Irc.ChannelMessage时会出错。
Private Sub irc_ChannelMessage(Channel As String, User As String, Message As String) Handles irc.ChannelMessage
rtbOutput.Clear()
rtbOutput.Text = Message
If rtbOutput.Text.StartsWith("!Listen ") Then
Dim s As String = rtbOutput.Text
Dim pars As New List(Of String)(s.Split(" "c))
CheckParams(pars)
End If
End Sub
当我的程序设置为使用.NET 4时,该库适用于我的程序,但当我将其设置为使用.Net 3.5 Client时,它会返回以下错误
错误4方法'Private Sub irc_ChannelMessage(Channel As String,User As String,Message As String)'无法处理事件'公共事件 ChannelMessage(发件人为对象,e为 TechLifeForum.ChannelMessageEventArgs)'因为他们没有 兼容的签名。
似乎一旦我为.NET 3.5编译它,IrcClient.cs就无法正确地将它转换为EventArguments.cs ......
IrcClient.cs中的我们正在调用
public event EventHandler<ChannelMessageEventArgs> ChannelMessage = delegate { };
然后应该从EventArguments.cs调用This:
public class ChannelMessageEventArgs : EventArgs
{
public string Channel { get; internal set; }
public string From { get; internal set; }
public string Message { get; internal set; }
public ChannelMessageEventArgs(string Channel, string From, string Message)
{
this.Channel = Channel;
this.From = From;
this.Message = Message;
}
}
但它只适用于.NET 4任何IDEAS?
答案 0 :(得分:2)
设置事件处理程序(Handles
关键字)时,方法的签名(换句话说,传递给方法的变量)必须与事件的签名匹配。
您的活动签名是(C#):
EventHandler<ChannelMessageEventArgs> ChannelMessage
(vb.net):
EventHandler(EventArgs as ChannelMessageEventArgs)
您错误地尝试使用以下签名处理此事件:
irc_ChannelMessage(Channel As String, User As String, Message As String)
这三个变量包含在ChannelMessageEventArgs类中,并一起传递,因此您可以将事件处理方法更改为:
Private Sub irc_ChannelMessage(EventArgs As ChannelMessageEventArgs) Handles irc.ChannelMessage
rtbOutput.Clear()
rtbOutput.Text = EventArgs.Message
If rtbOutput.Text.StartsWith("!Listen ") Then
Dim s As String = rtbOutput.Text
Dim pars As New List(Of String)(s.Split(" "c))
CheckParams(pars)
End If
End Sub