加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:57:12 所属栏目:百科 来源:网络整理
导读:我有以下界面: public interface INotificationHandlerT{ TaskT Handle(string msg);} 还有几个类很高兴地实现它: public class FooHandler : INotificationHandlerFoo{ public TaskFoo Handle(string msg) { return Task.FromResultFoo(new Foo()); }}pub
我有以下界面:
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 */)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读