The goal
Constructing a concrete object implementing ICoolbag
and only being able to store ICoolBagGrocery
instead of any type of grocery with IGrocery
.
The problem
The implementation below results in the following error:
The type 'SolidDistribution.Core.Bag.CoolBag.ICoolBag' cannot be used as type parameter 'T'
in the generic type or method 'IBagStorage<T>'. There is no implicit reference conversion from
'SolidDistribution.Core.Bag.CoolBag.ICoolBag' to 'SolidDistribution.Core.Bag.IBag<SolidDistribution.Core.IGrocery>'.
Implementation
Bag
// A bag that can only hold coolbag groceries.
public interface ICoolBag : IBag<ICoolBagGrocery> { }
// a bag that can hold any type of grocery.
public interface IBag<T> : IGroceryStorage<T> where T : IGrocery { }
Storage
// A storage that can store any type of grocery.
public interface IGroceryStorage<T> : IStorage<T> where T : IGrocery { }
// A storage that can store any type of bag.
public interface IBagStorage<T> : IStorage<T> where T : IBag<IGrocery> { }
// Base storage interface.
public interface IStorage<T> { }
Grocery
// A grocery that can only be stored in a coolbag.
public interface ICoolBagGrocery : IGrocery { }
// Base grocery interface.
public interface IGrocery { }
Box
// A box with a bag that can only hold coolbag groceries.
public interface ICoolBox : IBoxWithBag<ICoolBag> { }
// Base box with bag storage interface.
public interface IBoxWithBag<T> : IBox, IBagStorage<T> where T : IBag<IGrocery> { }
// Base box interface.
public interface IBox { }
Note
Changing ICoolbag
to use IGrocery
instead of ICoolBagGrocery
like so:
(public interface ICoolBag : IBag<IGrocery> { }
) fixes the error, but at the same time enables the ability to put any type of grocery in a coolbag. Which is obviously not supposed to happen :)