为什么这不起作用?!
public interface IBus
{
void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent;
void Send<T>(IEvent @event) where T : class, IEvent;
}
class InMemoryEventBus : IBus
{
private readonly IDictionary<ISubscribe<IEvent>, Type> _subscribers;
public InMemoryEventBus()
{
_subscribers= new Dictionary<ISubscribe<IEvent>, Type>();
}
public void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent
{
_subscribers.Add(subscriber, typeof(T));
}
public void Send<T>(IEvent @event) where T : class, IEvent
{
foreach (var subscriber in _subscribers.Where(subscriber => subscriber.Value == typeof(T)))
{
subscriber.Key.Handle(@event);
}
}
}
public interface IEvent
{
Guid EventId { get; set; }
}
public interface ISubscribe<T> where T : IEvent
{
void Handle(T @event);
}
public class StockLevelDroppedBellowMinimumLevelEvent : IEvent
{
public Guid EventId { get; set; }
public string Message { get; set; }
}
我得到:
cannot convert from 'IHandle<T>' to 'IHandle<IEvent>'