首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法将派生类型隐式转换为其基泛型类型

无法将派生类型隐式转换为其基泛型类型
EN

Stack Overflow用户
提问于 2012-09-08 03:09:28
回答 3查看 19.7K关注 0票数 28

我有以下类和接口:

public interface IThing
{
    string Name { get; }
}

public class Thing : IThing
{
    public string Name { get; set; }
}

public abstract class ThingConsumer<T> where T : IThing
{
    public string Name { get; set; }
}

现在,我有一个工厂,它将返回从ThingConsumer派生的对象,如下所示:

public class MyThingConsumer : ThingConsumer<Thing>
{
}

我的工厂现在看起来像这样:

public static class ThingConsumerFactory<T> where T : IThing
{
    public static ThingConsumer<T> GetThingConsumer(){
        if (typeof(T) == typeof(Thing))
        {
            return new MyThingConsumer();
        }
        else
        {
            return null;
        }
    }
}

我遇到了这个错误:Error 1 Cannot implicitly convert type 'ConsoleApplication1.MyThingConsumer' to 'ConsoleApplication1.ThingConsumer<T>'

有人知道如何完成我在这里尝试的东西吗?

谢谢!

克里斯

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12324020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档