Cannot implicitly convert derived type to its base generic type

后端 未结 3 1831
北海茫月
北海茫月 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:44

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

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

    Edit

    One more change needed. In ThingConsumerFactory, cast back to the return type IThingConsumer:

    return (IThingConsumer)new MyThingConsumer();
    

提交回复
热议问题