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

时间:2022-11-25 16:30:32

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

我遇到了这个错误:错误1无法将类型'ConsoleApplication1.MyThingConsumer'隐式转换为'ConsoleApplication1.ThingConsumer '

Anyone know how to accomplish what I'm attempting here?

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

Thanks!

谢谢!

Chris

克里斯

3 个解决方案

#1


8  

If you make ThingConsumer<T> an interface rather than an abstract class, then your code will work as is.

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

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

还需要一个改变。在ThingConsumerFactory中,强制转换为返回类型IThingConsumer

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

#2


4  

The compiler is stumbling over the conversion from MyThingConsumer to ThingConsumer<T> even though T:IThing and MyThingConsumer:Thingconsumer<Thing> and Thing:IThing. Which is quite a few hoops for it to jump through!

编译器绊倒了从MyThingConsumer到ThingConsumer 的转换,即使T:IThing和MyThingConsumer:Thingconsumer 和Thing:IThing。它有很多可以跳过的箍!

The code works if you use 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.

如果您使用返回的新MyThingConsumer()作为ThingConsumer ,则代码有效;而不是直接演员。您知道结果永远不会为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);

#3


1  

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已经在其定义中输入了:其中T:IThing

Now, you can make the call return new MyThingConsumer<T>();.

现在,您可以使调用返回new MyThingConsumer ();.

This should in turn match the expected return type of ThingConsumer<T>

这应该与ThingConsumer 的预期返回类型相匹配

EDIT

编辑

Sorry for the confusion, here is what should work:

抱歉混淆,这是应该工作的:

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

and

return new MyThingConsumer<T>();

#1


8  

If you make ThingConsumer<T> an interface rather than an abstract class, then your code will work as is.

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

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

还需要一个改变。在ThingConsumerFactory中,强制转换为返回类型IThingConsumer

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

#2


4  

The compiler is stumbling over the conversion from MyThingConsumer to ThingConsumer<T> even though T:IThing and MyThingConsumer:Thingconsumer<Thing> and Thing:IThing. Which is quite a few hoops for it to jump through!

编译器绊倒了从MyThingConsumer到ThingConsumer 的转换,即使T:IThing和MyThingConsumer:Thingconsumer 和Thing:IThing。它有很多可以跳过的箍!

The code works if you use 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.

如果您使用返回的新MyThingConsumer()作为ThingConsumer ,则代码有效;而不是直接演员。您知道结果永远不会为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);

#3


1  

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已经在其定义中输入了:其中T:IThing

Now, you can make the call return new MyThingConsumer<T>();.

现在,您可以使调用返回new MyThingConsumer ();.

This should in turn match the expected return type of ThingConsumer<T>

这应该与ThingConsumer 的预期返回类型相匹配

EDIT

编辑

Sorry for the confusion, here is what should work:

抱歉混淆,这是应该工作的:

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

and

return new MyThingConsumer<T>();