title: c# - cast generic class to its base non-generic class
body: <p>I have following classes:</p>
  
  <pre><code>public abstract class CustomerBase
  {
      public long CustomerNumber { get; set; }
      public string Name { get; set; }
  }
  
  public abstract class CustomerWithChildern&lt;T&gt; : CustomerBase
      where T: CustomerBase
  {
      public IList&lt;T&gt; Childern { get; private set; }
  
      public CustomerWithChildern()
      {
          Childern = new List&lt;T&gt;();
      }
  }
  
  public class SalesOffice : CustomerWithChildern&lt;NationalNegotiation&gt;
  {
  }
  </code></pre>
  
  <p>The SalesOffice is just one of few classes which represent different levels of customer hierarchy. Now I need to walk through this hierarchy from some point (CustomerBase). I can't figure out how to implement without using reflection. I'd like to implement something like:</p>
  
  <pre><code>    public void WalkHierarchy(CustomerBase start)
      {
          Print(start.CustomerNumber);
          if (start is CustomerWithChildern&lt;&gt;)
          {
              foreach(ch in start.Childern)
              {
                  WalkHierarchy(ch);
              }
          }
      }
  </code></pre>
  
  <p>Is there any chance I could get something like this working?</p>
  
  <hr>
  
  <p>The solution based on suggested has-childern interface I implemented:</p>
  
  <pre><code>public interface ICustomerWithChildern
  {
      IEnumerable ChildernEnum { get; }
  }
  
  public abstract class CustomerWithChildern&lt;T&gt; : CustomerBase, ICustomerWithChildern
      where T: CustomerBase
  {
      public IEnumerable ChildernEnum { get { return Childern; } }
  
      public IList&lt;T&gt; Childern { get; private set; }
  
      public CustomerWithChildern()
      {
          Childern = new List&lt;T&gt;();
      }
  }
  
      public void WalkHierarchy(CustomerBase start)
      {
          var x = start.CustomerNumber;
          var c = start as ICustomerWithChildern;
          if (c != null)
          {
              foreach(var ch in c.ChildernEnum)
              {
                  WalkHierarchy((CustomerBase)ch);
              }
          }
      }
  </code></pre>