简体   繁体   English

将一个子类分配给一个接口,涉及泛型和协变量

[英]Assign a sub class to an interface, with generics and covariant involved

I defined 3 interfaces: 我定义了3个接口:

public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
    where S : IManufacturerInput
    where T : IManufacturerOutput
{
    T Calculate(S);
}

And I defined a specific Manufacturer: 我定义了一个特定的制造商:

public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
     public ManufacturerAOutput Calculate(ManufacturerAInput)
     {
         return null;
     }
}

And In Main() I created a ManufacturerAApi, and try assign it to IManufacturerApi. 在Main()中,我创建了一个ManufacturerAApi,然后尝试将其分配给IManufacturerApi。

IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();

But it failed. 但是失败了。 The error message said (just abstract meaning): 错误消息说(只是抽象含义):

Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>

So is there any way I can make the assignment work? 那么,有什么方法可以使作业生效? Thanks in advance. 提前致谢。

What you are proposing isn't type safe. 您提议的内容类型不安全。 Let's change the names of your types to make the issue clearer: 让我们更改类型的名称,以使问题更清楚:

public interface IPetFood { }
public interface IPetSound { }

public interface IPetCage<in S, out T>
    where S : IPetFood
    where T : IPetSound
{
    T Feed(S s);
}

public class DogFood : IPetFood { }
public class CatFood : IPetFood { }
public class Bark : IPetSound { }

public class DogCage : IPetCage<DogFood, Bark>
{
    public Bark Feed(DogFood input)
    {
        return new Bark();
    }
}

And now suppose this is legal: 现在假设这是合法的:

IPetCage<IPetFood, IPetSound> api = new DogCage();

Then we could do the following: 然后,我们可以执行以下操作:

api.Feed(new CatFood()); //oops we've just given the dog some catfood.

The assignment will not work because S is contravariant, which means that any possible IPetFood passed into api.Feed would need to be a subtype of DogFood and you have the opposite; 分配将不起作用,因为S是反IPetFood ,这意味着传递给api.Feed任何可能的IPetFood必须是DogFood的子类型,而您却相反。 IPetFood is a superset of DogFood . IPetFood是的超集DogFood

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

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