Cannot implicitly convert derived type to its base generic type

后端 未结 3 1825
北海茫月
北海茫月 2020-12-20 10:56

I have the following classes and interfaces:

public interface IThing
{
    string Name { get; }
}

public class Thing : IThing
{
    public string Name { get         


        
3条回答
  •  遥遥无期
    2020-12-20 11:45

    You need to define your class like this I believe:

    public class MyThingConsumer : ThingConsumer
    

    The reason is that ThingConsumer is already typed in its definition with this: where T : IThing

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

    This should in turn match the expected return type of ThingConsumer

    EDIT

    Sorry for the confusion, here is what should work:

    public class MyThingConsumer : ThingConsumer where T : IThing
    

    and

    return new MyThingConsumer();
    

提交回复
热议问题