How can I instantiate this class?

c#, instantiation

Solution

You can instantiate that monster. All you have to do is create your own class that inherits from `Node`:

public class MyNode : BaseNode<MyNode>.Node
{
}

Then you can instantiate it like this:

BaseNode<MyNode> obj = new BaseNode<MyNode>();

Why you would want to do this, though, is a different matter entirely...

Problem

Consider this declaration with generics: ``` public class BaseNode<TNode> where TNode : BaseNode<TNode> { public class Node : BaseNode<Node> { public Node() { } } } ``` Is there a way to create an instance of class `Node` from outside the base class? I have used this pattern before, but always leaving the derived classes outside of the base class. How do you write the following without a compiler error? ``` var obj = new BaseNode<Node>.Node(); // error CS0246: The type or namespace name 'Node' could not be found ``` Have I created an un-instantiable class? Can it be initialized via reflection?

Original source