我有以下界面:
public interface INotificationHandler<T>
{
Task<T> Handle(string msg);
}
有几个类很乐意实现它:
public class FooHandler : INotificationHandler<Foo>
{
public Task<Foo> Handle(string msg) { return Task.FromResult<Foo>(new Foo()); }
}
public class BarHandler : INotificationHandler<Bar>
{
public Task<Bar> Handle(string msg) { return Task.FromResult<Bar>(new Bar()); }
}
我想在集合中保留一组INotificationHandler实例,当我收到消息&#34; foo&#34;时,请使用FooHandler,&#34; bar&#34;获得BarHandler等......
var notificationHandlers = new Dictionary<string, INotificationHandler<object>>();
notificationHandlers["foo"] = new FooHandler();
notificationHandlers["bar"] = new BarHandler();
...
public void MessageReceived(string type, string msg)
{
INotificationHandler<object> handler = notificationHandlers[type];
handler.Notify(msg).ContinueWith((result) => /* do stuff with a plain object */)
}
但是这无法编译,因为我的通用没有通用的基类型,这是设计的。任何对象都应该能够从MessageReceived
中的INotificationHandler返回。
无法将
FooHandler
类型隐式转换为INotificationHandler<object>
。 存在显式转换(您是否错过了演员?)
我如何使用INotificationHandler<T>
以便我不需要关心其具体实现的泛型类型?
答案 0 :(得分:2)
如果您需要类型安全,可以使用以下层次结构。
public interface INotificationHandler
{
Task<object> Handle(string msg);
}
public abstract BaseHandler<T> : INotificationHandler
{
Task<object> INotificationHandler.Handle(string msg)
{
return Handle(msg);
}
public abstract Task<T> Handle(string msg);
}
public class FooHandler : BaseHandler<Foo>
{
public override Task<Foo> Handle(string msg) { return Task.FromResult<Foo>(new Foo()); }
}
public class BarHandler : BaseHandler<Bar>
{
public override Task<Bar> Handle(string msg) { return Task.FromResult<Bar>(new Bar()); }
}
var notificationHandlers = new Dictionary<string, INotificationHandler>();
notificationHandlers["foo"] = new FooHandler();
notificationHandlers["bar"] = new BarHandler();
...
public void MessageReceived(string type, string msg)
{
INotificationHandler handler = notificationHandlers[type];
handler.Notify(msg).ContinueWith((result) => /* do stuff with a plain object */)
}