title: Generics and Parent/Child architecture
body: <p>I'm building an architecture with <strong>inheritable</strong> generics and parent-children relations.
  I have one major problem: I can't make both the child and the parent aware of each other's type, only one of the two.</p>
  
  <p>I need both the child and the parent to be aware of each other's type.</p>
  
  <p><strong>Scenario 1</strong>: Parent knows child type, but child only knows generic parent with generic children.</p>
  
  <pre><code>public class Child
  {
      public Parent&lt;Child&gt; Parent;
  }
  
  public class Parent&lt;TChild&gt;
      where TChild : Child
  {
      public List&lt;TChild&gt; Children;
  }
  </code></pre>
  
  <p><strong>Scenario 2</strong>: Child knows parent type, but parent only knows generic children with generic parent.</p>
  
  <pre><code>public class Child&lt;TParent&gt;
      where TParent : Parent
  {
      public TParent Parent;
  }
  
  public class Parent
  {
      public List&lt;Child&lt;Parent&gt;&gt; Children;
  }
  </code></pre>
  
  <p><strong>Scenario 3</strong>: The utopic but unachievable scenario:</p>
  
  <pre><code>public class Child&lt;TParent&gt;
      where TParent : Parent
  {
      public TParent Parent;
  }
  
  public class Parent&lt;TChild&gt;
      where TChild : Child
  {
      public List&lt;TChild&gt; Children;
  }
  </code></pre>
  
  <p>Of course, scenario 3 won't compile, because Parent and Child take a second generic type that would be <em>their own</em> type, but I can't (or at least don't know how!) to specify it is their own type.</p>
  
  <p>I'm falling in some kind of a infinite loop/recursion/ball-throw here, please help me before I drown.</p>
  
  <p>Good luck.</p>
  
  <p><strong>EDIT</strong>: If I wasn't clear, I can reference my types yes, but if the user derives Child into FooChild, and accesses the children of the parent of FooChild, these will only be Child, and not FooChild, as it should be. </p>
  
  <p><strong>Failure Example with Scenario 1</strong>:</p>
  
  <pre><code>public class Child
  {
      public Parent&lt;Child&gt; Parent;
  }
  
  public class Parent&lt;TChild&gt;
      where TChild : Child, new()
  {
      public List&lt;TChild&gt; Children;
  }
  
  public class FooChild : Child
  {
      public int Required;
  
      public void Bar()
      {
          foreach (Child child in this.Parent.Children)
          {
              int x = child.Required; // cannot be accessed!
          }
      }
  }
  </code></pre>
  
  <p><strong>Failure Example with Scenario 2</strong>:</p>
  
  <pre><code>public class Child&lt;TParent&gt;
      where TParent : Parent
  {
      public TParent Parent;
  }
  
  public class Parent
  {
      public List&lt;Child&lt;Parent&gt;&gt; Children;
  }
  
  public class FooChild : Child&lt;Parent&gt;
  {
      public int Required;
  
      public void Bar()
      {
          foreach (Child&lt;Parent&gt; child in this.Parent.Children)
          {
              int x = child.Required; // cannot be accessed!
          }
      }
  }
  </code></pre>