Q: Casting DerivedA to BaseA I am writing an event system and have hit a stumbling block when trying to store events of different generic types.
Events are defined like so:
public delegate void EventHandler<EventInfo>(object sender, EventInfo eventInfo) where EventInfo : IEventInfo;

public interface IEventInfo { }

public interface IEvent<EventInfo> where EventInfo : IEventInfo
{
    void Publish(object sender, EventInfo e);
    void Subscribe(EventHandler<EventInfo> evtHandler);
    void Unsubscribe(EventHandler<EventInfo> evtHandler);
}

public class Event<EventInfo> : IEvent<EventInfo> where EventInfo : IEventInfo
{
    private event EventHandler<EventInfo> eventHandler;

    public void Publish(object sender, EventInfo e)
    {
        if (eventHandler != null)
            eventHandler(sender, e);
    }

    public void Subscribe(EventHandler<EventInfo> evtHandler)
    {
        this.eventHandler -= evtHandler;
        this.eventHandler += evtHandler;
    }

    public void Unsubscribe(EventHandler<EventInfo> evtHandler)
    {
        this.eventHandler -= evtHandler;
    }
}

This model seems to work for subscribing and publishing, but the main reason I have implemented my own event class is so that I can use it with an event aggregator, allowing me to decouple my components:
public class EventAggregator
{
    private IDictionary<Type, IEvent<IEventInfo>> dictionary;

    public EventAggregator()
    {
        this.dictionary = new Dictionary<Type, IEvent<IEventInfo>>();
    }

    public EventType GetEvent<EventType>() where EventType : class, IEvent<IEventInfo>, new()
    {
        IEvent<IEventInfo> evt;
        Type evtType = typeof(EventType);
        if (!dictionary.TryGetValue(evtType, out evt))
            dictionary[evtType] = evt = new EventType();
        return evt as EventType;
    }
}

The above function does not work with derived EventInfo classes. For example:
public class FooEventInfo : IEventInfo { }
public class FooEvent : Event<FooEventInfo> { }

EventAggregator ea = new EventAggregator();
var x = ea.GetEvent<FooEvent>(); // Error 3: The type 'TestEvent' cannot be used as type parameter 'EventType' in the generic type or method 'EventAggregator.GetEvent<EventType>()'. There is no implicit reference conversion from 'TestEvent' to 'IEvent<IEventInfo>'.

Why does this cast fail, given that TestEvent : Event<TestEventInfo>, Event<TestEventInfo> : IEvent<TestEventInfo> and TestEventInfo : IEventInfo?

A: It sounds to me like you're running into the fact that C# doesn't support covariance.  Take a look at this post to read about covariance/contravariance in C#, and an (ugly) workaround that he's come up with.
Here is a deeper explanation of the concepts at question.

A: Because Event<FooEventInfo> is not compatible with IEvent<IEventInfo.
If you have a type T' derived from T, it does not mean that A<T'> is derived from or in any way compatible to A<T>!
Take this example
class Animal { ... }
class Dog : Animal { ... }
class Cat : Animal { ... }

List<Animal> animals = new List<Dog>(); // Seems to be possible
animals.Add(new Dog());
animals.Add(new Cat()); // Crashhhh!

Adding a Cat to animals should be possible, but because the list is in reality a list of dogs, you would be adding a Cat into a Dog-list! Therefore the two types cannot be compatible!
