为什么不起作用?!
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>'
最佳答案
编辑:实际上是ISubscribe
这需要协变-但现在我们可以看到ISubscribe
的声明,您不能使其成为协变的-仅是协变的。
通常,类型不是通用变体。例如,ICollection<string>
不是ICollection<object>
-这是一项好工作,否则它将编译为:
ICollection<string> strings = new List<string>();
ICollection<object> objects = strings; // Fortunately this isn't valid
objects.Add(new Button()); // This should be fine of course...
string x = strings[0]; // But what would this do?!
对于您的情况,我相信您需要更改字典-您将能够保证在每种情况下都使用“正确”类型的
ISubscribe
。但是,您使用字典的方式很奇怪-您应该反转键和值类型,如下所示:class InMemoryEventBus : IBus
{
private readonly IDictionary<Type, object> _subscribers;
public InMemoryEventBus()
{
_subscribers = new Dictionary<Type, object>();
}
public void Subscribe<T>(ISubscribe<T> subscriber) where T : IEvent
{
_subscribers.Add(typeof(T), subscriber);
}
public void Send<T>(IEvent @event) where T : class, IEvent
{
object value;
if (_subscribers.TryGetValue(typeof(T), out value))
{
ISubscriber<T> subscriber = (ISubscriber<T>) value;
subscriber.Handle(@event);
}
}
}