title: Generic non-binary tree with different types per level
body: <p>I am trying to create a generic object hierarchy tree that allows for object lookup by name and id, as well as traversal. The base class is as follows:</p>
  
  <pre><code>public abstract class RuntimeObject
  {
    public Guid Id {get;}
    public string Name {get;}
    public Type ObjectType {get;}
  
    protected RuntimeObject( string name )
    {
      Id = Guid.NewGuid();
      Name = name;
    }
  }
  </code></pre>
  
  <p>This class is intended to be inherited by classes that are a part of the hierarchy.</p>
  
  <p>Next, I would like to expand on this to add support for navigating between parents and children while keeping things generic and not requiring top-level parents to define a <code>Parent</code> field, nor requiring leaf objects to define child fields. Trying to do so becomes very messy, and my design approach is awful:</p>
  
  <pre><code>// Top-level parent class
  public abstract class RuntimeObject&lt;TChild&gt; : RuntimeObject
    where TChild : RuntimeObject&lt;RuntimeObject&lt;TChild&gt;&gt;
  {
    public int ChildCount {get;}
    public TChild FirstChild {get;}
  }
  
  // Middle-level class
  public abstract class RuntimeObject&lt;TParent,TChild&gt; : RuntimeObject
    where TParent : RuntimeObject&lt;RuntimeObject&lt;TParent,TChild&gt;&gt;
    where TChild : RuntimeObject&lt;RuntimeObjecT&lt;TParent,TChild&gt;&gt;
  {
    public TParent Parent {get;}
    public RuntimeObject&lt;TParent,TChild&gt; NextSibling {get;}
  
    public int ChildCount {get;}
    public TChild FirstChild {get;}
  }
  
  // Leaf class
  public abstract class RuntimeObject&lt;TParent&gt; : RuntimeObject
    where TParent : RuntimeObject&lt;RuntimeObject&lt;TParent&gt;&gt;
  {
    public TParent Parent {get;}
    public RuntimeObject&lt;TParent&gt; NextSibling {get;}
  }
  </code></pre>
  
  <p>Not only is it an eyesore to read, but it also flat-out doesn't work.</p>
  
  <p><strong>Goal</strong>: Let <code>A</code> be a top-level parent, <code>C</code> be a leaf, and <code>B</code> be a child of A and have childen of type C. Ideally, implementing the hierarchy would look something along the lines of:</p>
  
  <pre><code>public class A : RuntimeObject&lt;B&gt;
  { ... }
  
  public class B : RuntimeObject&lt;A,C&gt;
  { ... }
  
  public class C : RuntimeObject&lt;B&gt;
  { ... }
  </code></pre>
  
  <p>This implementation does not work, but the idea stands: I would like to <strong>create a generic system for implementing a traversable hierarchy of objects</strong>, and would like to <strong>avoid requiring concrete classes to define <code>Parent</code> or <code>Child</code> fields if they are not needed.</strong></p>
  
  <p>Is there a design pattern to accomplish this? Does anyone have any suggestions for how it should be structured?</p>