views:

22

answers:

3

I have a method on an object oriented database that creates tables based on their type.

I want to be able to send a list of types to get created, but i'm hoping to limit them to only classes derived from a specific base class (MyBase).

Is there a way i can require this in the method signature? Instead of

CreateTables(IList<Type> tables)

Can i do something that would

CreateTables(IList<TypeWithBaseTypeMyBase> tables)

I know i could check the base class of each type sent over, but if possible i'd like this verified at compile time.

Any suggestions?

A: 

Have you tried this?

CreateTables(IList<MyBase> tables) 

I think that's all you have to do.

recursive
+1  A: 

You could do the following:

CreateTables(IList<MyBase> tables)
{
    // GetType will still return the original (child) type.
    foreach(var item in tables)
    {
        var thisType = item.GetType();
        // continue processing
    }
}
Justin Niessner
A: 

Why not just change the signature to:

CreateTables(IList<BaseType> tables)
dcp
There are no requirements on the calling side to have the classes instantiated. It seems like a waste to instantiate in order to use the type.
rediVider
I'm not sure I understand. If you are passing an IList<SomeType> to a method, then I assume the elements of the list will be instantiated classes of SomeType. All I'm saying is that you can use BaseType as the type for the IList as long as all your derived types inherit from it.
dcp
I'm not getting a list of objects. I'm getting a list of types.
rediVider