将一个sub class分配给一个接口,涉及泛型和协变
Assign a sub class to an interface, with generics and covariant involved
我定义了3个接口:
public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
where S : IManufacturerInput
where T : IManufacturerOutput
{
T Calculate(S);
}
我定义了一个特定的制造商:
public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
public ManufacturerAOutput Calculate(ManufacturerAInput)
{
return null;
}
}
然后在 Main() 中我创建了一个 ManufacturerAApi,并尝试将其分配给 IManufacturerApi。
IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();
但是失败了。错误信息说(只是抽象的意思):
Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>
那么有什么方法可以使作业生效吗?提前致谢。
您提出的建议类型不安全。让我们更改您的类型的名称以使问题更清楚:
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();
}
}
现在假设这是合法的:
IPetCage<IPetFood, IPetSound> api = new DogCage();
然后我们可以执行以下操作:
api.Feed(new CatFood()); //oops we've just given the dog some catfood.
赋值将不起作用,因为 S
是逆变的,这意味着传递给 api.Feed
的任何可能的 IPetFood
都需要是 DogFood
的子类型,而您有相反的; IPetFood
是 DogFood
的超集。
我定义了3个接口:
public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
where S : IManufacturerInput
where T : IManufacturerOutput
{
T Calculate(S);
}
我定义了一个特定的制造商:
public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
public ManufacturerAOutput Calculate(ManufacturerAInput)
{
return null;
}
}
然后在 Main() 中我创建了一个 ManufacturerAApi,并尝试将其分配给 IManufacturerApi。
IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();
但是失败了。错误信息说(只是抽象的意思):
Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>
那么有什么方法可以使作业生效吗?提前致谢。
您提出的建议类型不安全。让我们更改您的类型的名称以使问题更清楚:
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();
}
}
现在假设这是合法的:
IPetCage<IPetFood, IPetSound> api = new DogCage();
然后我们可以执行以下操作:
api.Feed(new CatFood()); //oops we've just given the dog some catfood.
赋值将不起作用,因为 S
是逆变的,这意味着传递给 api.Feed
的任何可能的 IPetFood
都需要是 DogFood
的子类型,而您有相反的; IPetFood
是 DogFood
的超集。