OK, that title is a little unclear, but I can't think of a better way of putting it, other than explaining it...
Say I have a class Animal
, with a static, generic method:
public static T Create<T>() where T : Animal {
// stuff to create, initialize and return an animal of type T
}
And I have subclasses Dog
, Cat
, Hamster
etc. In order to get a Dog
, I can write:
Dog d = Animal.Create<Dog>();
or
Dog d = Dog.Create<Dog>();
which is really the same thing. But it seems kinda silly to have to write Dog
so many times, since I'm already invoking the static method through the Dog
subclass.
Can you think of any clever way of writing a Create()
method in the base class so that I could invoke
Dog d = Dog.Create();
Cat c = Cat.Create();
Hamster h = Hamster.Create();
without writing a Create()
method in each of the subclasses?