I have something I'm trying to make work with generics, and it seems
like it should, but I can't quite get there.
I have a subclass of List<Tthat I want to populate automatically.
Each different T is populated with a different set of data. Some
sample code is given below - which clearly doesn't work, because you
can't translate back and forth between the types.
Can anyone tell me the right way to solve this kind of problem?
public static List<TGetPopulatedList<T>()
{
List<TmyList = new List<T>();
if (typeof(T) == typeof(Dog))
{
PopulateListWithDogs(myList);
}
if (typeof(T) == typeof(Cat))
{
PopulateListWithCats(myList);
}
}
private static void PopulateListWithDogs(List<DogdogList)
{
dogList.Add(new Dog("Labrador"));
dogList.Add(new Dog("Alsatian"));
}
private static void PopulateListWithCats(List<CatcatList)
{
catList.Add(new Cat("Siamese"));
catList.Add(new Cat("Persian"));
}
All I want to do is pass in a type and get a list back of all the
possible options for that type. (And no, my actual app doesn't work
with cats and dogs, this is just an example)
Cheers for any help you can give,
Andy