I have some classes that use inheritance as in
Teacher : Person : Entity : BaseObject
Student : Person : Entity :BaseObject
Company : Entity : BaseObject etc.
The base object has an abstract IEnumerable property called Hierarchy
Entity class implements Hierarchy as new List<Type>() { typeof(Entity) };
Person class implements Hierarchy as new List<Type> { typeof(Person), typeof(Entity)};
etc.
In my db.Save method I am trying to cast the object to each of these types in turn so I can call the correct stored procedure.
public static void SaveObject(BaseObject obj)
{
foreach (var type in obj.Hierarchy) //obj.Hierarchy is IEnumerable<Type>
{
Object o = obj as type;
//.....
}
}
but I get an error 'type is a variable but is used like a type'. Is there any way I can cast my child object to one of its parent types along these lines?