I’ve created a generic wrapper type (AnimalVehicleWrapper in the example below). I’ve created two separate derived types, one which holds “parent” types (animal and vehicle) and one that holds more derived types (dog and car). I’m wondering if it’s possible to create a type hierarchy such that I could create a method that would define an incoming ParentWrapper
parameter, which would all me to pass either that type OR the more derived DerivedWrapper
.
Thank you.
using Example;
namespace Example
{
public class Animal
{
public string Name { get; set; }
}
public class Dog : Animal
{
int PawSize { get; set; }
}
public class Vehicle
{
public string Name { get; set; }
}
public class Car : Vehicle
{
public int TireCount { get; set; }
}
public class AnimalVehicleWrapper<T1, T2>
where T1 : Animal
where T2 : Vehicle
{
public virtual T1 AnimalOrDerived { get; set; }
public virtual T2 VehicleOrDerived { get; set; }
}
}
public class ParentWrapper : AnimalVehicleWrapper<Animal, Vehicle>
{
}
public class DerivedWrapper : AnimalVehicleWrapper<Dog, Car>
{
}
…
public static void DoProcessing(ParentWrapper wrapper)
{
// Would like to be able to pass in a ParentWrapper OR a DerivedWrapper here
}