3

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...

1
  • I thought I had the answer but you're right - you don't want a list of IBase-implementing objects, you want a list of TYPE's that implement that interface. I think you have to implement your own list with extra logic in Add Commented Nov 7, 2012 at 20:54

3 Answers 3

4

typeof(IBase), typeof(object), typeof(Foo), all return an instance of Type, 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:

List<typeof(IBase)> items;

(i don't even know if this compiles ? ) Is exactly the same as this:

List<Type> items;

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:

public class MyTypeList<T> : Collection<Type>
{
    protected override InsertItem( int index, Type item )
    {
        if( !typeof(T).IsAssignableFrom(item) )
        {
            throw new ArgumentException("the Type does not derive from ... ");
        }

        base.InsertItem(index, item);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is basically what I ended up doing. I was just wondering if there was a cleverer way to do it. For example using a where clause or something...
And I don't think it's useless. I want a list of types where all types in the list must implement a certain interface.
And what are you going to do with that list ?
1

Yes. You have to implement a List that throws exceptions if type is not a subclass from IBase.

There is no built in way to do what you want.

Comments

1

The only way to do that is to create your own type collection

public class MyTypeList
{
    List<Type> _innerList;

    public void Add(Type type)
    {
        if (typeof(IBase).IsAssignableFrom(type)) {
             _innerList.Add(type);
        } else {
            throw new ArgumentException(
                "Type must be IBase, implement or derive from it.");
        }
    }

    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.