I have a class in C # with a template and a static method similar to
class BClass<T>
{
public static BClass<T> Create()
{
return new BClass<T>();
}
}
From this I get the class and set the template parameter to the base class
class DClass : BClass<int> { }
The problem occurs when I try to use the static method to instantiate D
class Program
{
static void Main(string[] args)
{
DClass d = DClass.Create();
}
}
Gives a compiler error "Unable to implicitly convert the type" Test.BClass <int> "to" Test.DClass ".
Adding the following list throws a runtime exception at runtime.
DClass d = (DClass)DClass.Create();
Is there a succint method that allows a static method to instantiate a derived class? Ideally, I would like to get the C ++ equivalent of typedef, and I don't need the syntax below (which works).
BClass<int> d = DClass.Create();