I’d like to create a list of types, each of which must implement a particular interface. Like:
interface IBase { }
interface IDerived1 : IBase { }
interface IDerived2 : IBase { }
class HasATypeList
{
List<typeof(IBase)> items;
HasATypeList()
{
items.Add(typeof(IDerived1));
}
}
So I know I can do
List<Type> items;
But that won’t limit the allowable types in the list to ones that implement IBase. Do I have to write my own list class? Not that it’s a big deal, but if I don’t have to…
typeof(IBase),typeof(object),typeof(Foo), all return an instance ofType, with the same members and so on.I don’t see what you’re trying to achieve and why you want to make a distinction between those ?
In fact, the code you’re writing here:
(i don’t even know if this compiles ? )
Is exactly the same as this:
So in fact, what you’re trying to achieve is imho useless.
If you really want to achieve this -but I do not see why … -, you can always create your own collection-type like Olivier Jacot-Descombes is suggesting, but in that case, I’d rather create a type that inherits from
Collection<T>instead: