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?
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
}
}
Have you tried this?
CreateTables(IList<MyBase> tables)
I think that's all you have to do.
Why not just change the signature to:
CreateTables(IList<BaseType> tables)
精彩评论