无法在C#中将具体类型转换为其接口的通用版本

前端之家收集整理的这篇文章主要介绍了无法在C#中将具体类型转换为其接口的通用版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下界面:
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 to INotificationHandler<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 */)
}

猜你在找的C#相关文章