I have a base class that takes one general argument. Then I have several classes that inherit from this base class. Is there an easy way for child classes to embed a factory from a base class?
Example
class BaseClass<T>
{
T Value {get; set;}
string Name {get; set;}
public static BaseClass<T> Factory(T Value)
{
return new BaseClass<T>(Value);
}
}
class ChildClass : BaseClass<int>
{
public void Test()
{
ChildClass bs = ChildClass.Factory(10);
}
}
In the code, I noticed that I want to work. I can think of one way to overcome this by adding an implicit statement in BaseClass or SubClass that converts from BaseClass to ChildClass.
I can also just explicitly add a factory to ChildClass, but that defeats the point of inheritance.
Is there a better, more standardized way to do this?
source
share