简体   繁体   English

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

[英]Cannot implicitly convert derived type to its base generic type

I have the following classes and interfaces: 我有以下类和接口:

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

Now, I have a factory that will return objects derived from ThingConsumer like: 现在,我有一个工厂,它将返回从ThingConsumer派生的对象,例如:

public class MyThingConsumer : ThingConsumer<Thing>
{
}

My factory currently looks like this: 我的工厂目前看起来像这样:

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

I'm getting tripped up with this error: Error 1 Cannot implicitly convert type 'ConsoleApplication1.MyThingConsumer' to 'ConsoleApplication1.ThingConsumer<T>' 我因以下错误而绊倒: Error 1 Cannot implicitly convert type 'ConsoleApplication1.MyThingConsumer' to 'ConsoleApplication1.ThingConsumer<T>'

Anyone know how to accomplish what I'm attempting here? 有人知道我该怎么做吗?

Thanks! 谢谢!

Chris 克里斯

If you make ThingConsumer<T> an interface rather than an abstract class, then your code will work as is. 如果将ThingConsumer<T>为接口而不是抽象类,则您的代码将按原样运行。

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

Edit 编辑

One more change needed. 还需要进行一项更改。 In ThingConsumerFactory , cast back to the return type IThingConsumer<T> : ThingConsumerFactoryThingConsumerFactory转换为返回类型IThingConsumer<T>

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

The compiler is stumbling over the conversion from MyThingConsumer to ThingConsumer<T> even though T:IThing and MyThingConsumer:Thingconsumer<Thing> and Thing:IThing . 即使T:IThingMyThingConsumer:Thingconsumer<Thing>Thing:IThing ,编译器MyThingConsumer努力ThingConsumer<T>MyThingConsumerThingConsumer<T>的转换。 Which is quite a few hoops for it to jump through! 它需要跳很多圈!

The code works if you use return new MyThingConsumer() as ThingConsumer<T>; 如果使用return new MyThingConsumer() as ThingConsumer<T>;则该代码有效return new MyThingConsumer() as ThingConsumer<T>; instead of a direct cast. 而不是直接投放。 You know the result will never be null , and the compiler is happy because it is guaranteed a return value of the right type at runtime. 您知道结果永远不会为null ,并且编译器很高兴,因为可以保证在运行时返回正确类型的返回值。

Edit: Here is the full code I used for testing (in Snippy ): 编辑:这是我用于测试的完整代码(在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);

You need to define your class like this I believe: 我相信您需要像这样定义您的班级:

public class MyThingConsumer<Thing> : ThingConsumer

The reason is that ThingConsumer is already typed in its definition with this: where T : IThing 原因是ThingConsumer已经在其定义中输入了以下内容: where T : IThing

Now, you can make the call return new MyThingConsumer<T>(); 现在,您可以使调用return new MyThingConsumer<T>(); .

This should in turn match the expected return type of ThingConsumer<T> 这应与ThingConsumer<T>的预期返回类型匹配

EDIT 编辑

Sorry for the confusion, here is what should work: 很抱歉造成混淆,这是可行的方法:

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

and

return new MyThingConsumer<T>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM