I have a class Container <T> that has ContainerContents <T>. A container actually accepts two restriction parameters of the type Container <TContainer, TContents> - TContainer is the type of container, and TContents is the type of content that it accepts.
I want to make sure that if TContainer is X or derived from X, then TContents will also be X or derived from X, but TContents should not equal TContainer.
I am trying to express the following things.
- Things you can carry with you (Swag), like a pencil.
- Things that can't be wrapped (BaseObject), like a tree.
- Things that may contain other things (Container)
- Containers that cannot be transported like bank vault.
- Transportable containers (e.g. backpack).
If the container can be transported, its contents must also be loaded. But just because the Container is a backpack, this does not mean that it can only carry backpacks.
I want to be able to code:
var ringWorld = new Container<BigRing, CivicWork>();var pickleKnox = new Container<BankVault, Pickle>();var swagBag = new Container<ToteBag, Swag>();var tomeBag = new Container<ToteBag, Book>();
but not var treeBag = new Container<Bag, Tree>();
Here is my skeletal installation.
public abstract class BaseObject { private readonly string _name; protected BaseObject(string name) { _name = name; } public string Name { get { return _name; } } }
public class Swag : BaseObject { private readonly int _weight; public Swag(string name, int weight):base(name) { _weight = weight; } public int Weight { get { return _weight; } } }
public class Container<TContainer,TContents> : BaseObject where TContainer:BaseObject where TContents:BaseObject, or Swag if TContainer:(Swag or derived from Swag) { ContainerContents<TContents> _contents; public Container(string name, int maxItems):base(name) { _contents = new ContainerContents<TContents>(maxItems); } }
public class ContainerContents <T>: List <T> where T: BaseObject
{
int _maxItems;
public ContainerContents (int maxItems)
{
_maxItems = maxItems;
}
}
user164226
source share