public event EventHandler<SubEventArgs> MyEvent;
dictionary<string,EventHandler<BaseEventArgs>> Foo =new dictionary<string,EventHandler<BaseEventArgs>>();
Foo.Add("Item1" , MyEvent);
SubEventArgs 继承自 BaseEventArgs。
但这不起作用?如何处理?
更新:错误是
cannot convert from 'EventHandler<SubEventArgs>' to 'EventHandler<BaseEventArgs>'
The best overloaded method match for 'System.Collections.Generic.Dictionary<string,EventHandler<BaseEventArgs>>.Add(string, EventHandler<BaseEventArgs>)' has some invalid arguments
更新:基类和子类
public class BaseEventArgs : EventArgs
{
object[] data;
public BaseEventArgs(object[] data)
{
this.data = data;
}
public BaseEventArgs(List<object> data)
{
this.data = data.ToArray();
}
public object[] Data
{
get
{
return this.data;
}
}
public bool HasData()
{
return this.data.Count() > 0;
}
}
public class SubEventArgs : BaseEventArgs
{
public enum ErrorType {
Validation = 1
Logic = 2,
System = 3
};
private ErrorType typeOfError;
public SubEventArgs( ErrorType errorType, object[] data)
: base(data)
{
this.typeOfError = errorType;
}
public SubEventArgs(ErrorType errorType, List<object> data)
: base(data)
{
this.typeOfError = errorType;
}
public ErrorType TypeOfError
{
get
{
return this.typeOfError;
}
}
}