I am trying to create an abstract class (A) that returns an instance of a generic child of itself (B).
I made B's costructor internal, making the creation of a new instance without my factory impossible, this caused an error I cannot seem to find a solution for:
'A.B' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method A.Create().
Is there a solution for such a problem in C#?
namespace C
{
class P
{
static void Main() => Console.WriteLine(A.Create<A.B>().GetType());
}
public abstract class A
{
public class B : A { protected B() { } }
public static T Create<T>() where T : A, new() => new T();
}
}