I have an abstract base class and a derived class. Let us call them Base
and Derived
.
Base
has the constructor:
public Base(int number)
{
// do something with number
}
and the Derived
class will always have the constructor (even though this cannot be ensured through an interface, this is nevertheless the case in my program):
public Derived(int number) : base(number)
{
// do some other stuff
}
Now, I would like a factory method to create objects derived from Base
, which should be called like this:
Base.Create<Derived>(someNumber);
But I do not know how to implement this method in a correct fashion. I think the most elegant solution would be to do something like this:
public static T Create<T>(int number) where T : Base, new(int)
{
return new T(number);
}
But it seems that C# does not support the parameterized constructor constraint new(int)
, only the parameterless one. This solution would have been so clear and it expresses exactly what I want. Oh well..
Instead, I could basically switch on the type parameter and create and instance of the correct type like this:
public static Base Create<T>(int number) where T : Base, new(int)
{
if (typeof(T) == typeof(Derived))
{
return new Derived(number);
}
// and so on for all other derived types
}
But this requires me to update the method every single time I make a new derived class, which is unfortunate. Besides, typeof(T) == typeof(Derived)
seems way too hackish. Also, with this approach it seems the return type of the factory method will have to be Base
instead of T
, which is also unfortunate. I could just as easily resort to using enums instead of a type parameter in that case.
I wonder if there is a better way to achieve what I want?