I am trying the following method, but it gives me a compiler error:
public class MyManager<T> : where T:MyEventArgs
{
private Dictionary<EventHandler<T>, EventFilter<T>> m_cSubscriptions;
public void Subscribe<K>(EventHandler<K> _cHandler, EventFilter<K> _cFilter)
where K:T
{
try
{
// cannot convert EventHandler<K> to EventHandler<T>
m_cSubscriptions.Add(_cHandler, _cFilter);
}
catch (ArgumentException)
{
m_cSubscriptions[_cHandler] = _cFilter;
}
}
}
Question: Why can’t I convert the handler from K to T?
I am using a .net 2.0, because I am using Unity3D. Do I have to cast it myself? I have read about covariance and contravariance, but I couldn’t make sense of this one.
I’ve decided to change the dictionary to a more common one by using
Delegateand also defining a normalEventFilterclass without generics. I hope that the casting will be correct, since I haven’t tested this yet. I hope that it will work.