我有以下界面:
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实例,当我收到消息“foo”时,使用FooHandler,“bar”获取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返回任何对象.
Cannot implicitly convert type
FooHandler
toINotificationHandler<object>
.
An explicit conversion exists (are you missing a cast?)
如何使用INotificationHandler< T>所以我不需要关心其具体实现的泛型类型?
解决方法
如果需要类型安全性,可以使用以下层次结构.
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 */) }