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

时间:2012-09-07 19:09:28

标签: c# .net generics

我有以下类和接口:

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>'

任何人都知道如何完成我在这里尝试的事情?

谢谢!

克里斯

3 个答案:

答案 0 :(得分:9)

如果你使ThingConsumer<T>成为一个接口而不是一个抽象类,那么你的代码将按原样运行。

public interface IThingConsumer<T> where T : IThing
{
    string Name { get; set; }
}

修改

需要进行一项更改。在ThingConsumerFactory中,转回返回类型IThingConsumer<T>

return (IThingConsumer<T>)new MyThingConsumer();

答案 1 :(得分:4)

即使MyThingConsumerThingConsumer<T>以及T:IThing,编译器仍然会遇到从MyThingConsumer:Thingconsumer<Thing>Thing:IThing的转换。这是一个很难实现的目标!

如果您使用return new MyThingConsumer() as ThingConsumer<T>;而不是直接投射,则代码有效。您知道结果永远不会是null,并且编译器很高兴,因为它保证了在运行时返回正确类型的值。

修改 以下是我用于测试的完整代码(在Snippy中):

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; }
}

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() as ThingConsumer<T>;
        }
        else
        {
            return null;
        }
    }
}

...

var thing = ThingConsumerFactory<Thing>.GetThingConsumer();
Console.WriteLine(thing);

答案 2 :(得分:1)

你需要像我这样定义你的类:

public class MyThingConsumer<Thing> : ThingConsumer

原因是ThingConsumer已在其定义中输入:where T : IThing

现在,您可以拨打电话return new MyThingConsumer<T>();

这应该与ThingConsumer<T>

的预期回报类型相匹配

修改

对于这种困惑感到抱歉,这是应该工作的:

public class MyThingConsumer<T> : ThingConsumer<T> where T : IThing

return new MyThingConsumer<T>();