我遇到了一个事件上升的问题。这是我的基类:
public abstract class MyClass<T>
{
public event EventHandler<MessageReceivedEventArgs<T>> MessageReceived;
protected void OnMessageReceived(MyClass<T> sender, MessageReceivedEventArgs<T> e)
{
EventHandler<MessageReceivedEventArgs<T>> handler = MessageReceived;
if (MessageReceived != null)
MessageReceived(sender, e);
}
}
我的实现:
public class MyDerivedClass: MyClass<string>
{
void AFunction()
{
// Do some stuff
OnMessageReceived(this, new MessageReceivedEventArgs<string>("data"));
}
当OnMessageReceived
被调用时,系统抛出一个System.ArgumentNullException
(“值不能为空。参数名称:类型”)。我已经阅读了这个帖子,还有这个帖子,这有助于我构建这段代码。但我找不到这些异常的原因(sender
并且e
不为空,用调试器检查)。
我究竟做错了什么 ?
编辑:这是我的MessageReceivedEventArgs
实现:
public class MessageReceivedEventArgs<T> : EventArgs
{
public T Data { get; private set; }
public MessageReceivedEventArgs(T data)
{
Data = data;
}
}