title: Pass Generic Type up the Inheritance Tree
body: <p>I have the following classes:</p>
  
  <p><code>abstract class Foo&lt;T&gt;</code><br>
  <code>abstract class BaseClass : Foo&lt;BaseClass&gt;</code><br>
  <code>class DerivedOne : BaseClass</code><br>
  <code>class DerivedTwo : BaseClass</code></p>
  
  <p>Using this structure, all class that derive from <code>BaseClass</code> inherit from <code>Foo&lt;BaseClass&gt;</code>. I want to be able to set the generic type of <code>T</code> different in each derived class</p>
  
  <p>For example, in <code>DerivedOne</code> I want the class to be <code>Foo&lt;DerivedOne&gt;</code> but in <code>DerivedTwo</code> I want the class to derive from <code>Foo&lt;DerivedTwo&gt;</code>.</p>
  
  <p>Is there any way to pass the generic type up the inheritance tree?</p>
  
  <p>I tried doing something like this (which doesn't work).</p>
  
  <p><code>abstract class Foo&lt;T&gt;</code><br>
  <code>abstract class BaseClass&lt;T&gt; : Foo&lt;T&gt;</code><br>
  <code>class DerivedOne : BaseClass&lt;DerivedOne&gt;</code><br>
  <code>class DerivedTwo : BaseClass&lt;DerivedTwo&gt;</code></p>
  
  <p>Edit: changed generic types to show that they are passing in themselves as generic types</p>
  
  <p>Edit #2: Here is a code sample of the actual classes I am using:</p>
  
  <pre><code>public abstract class DBObject&lt;T&gt; 
      where T : DBObject&lt;T&gt;, new()
  { 
      //the generic type is for this method, so I can create an object from a database query
      public static T Create(DataRow dataRow)
      {
          Type T = MethodBase.GetCurrentMethod().DeclaringType;
          T obj = new T();
  
          foreach (var field in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
          {
              if (dataRow.Table.Columns.Contains(field.Name))
              {
                  Object safeObj = Convert.ChangeType(dataRow[field.Name].ToString(), field.FieldType);
                  field.SetValue(obj, safeObj);
              }
  
          }
  
          foreach (var field in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
          {
              Console.WriteLine(field.Name + "=" + field.GetValue(obj));
              //propInfo.SetValue(obj, value, null);
          }
          return obj;            
      }
  }
  
  public abstract class Contact&lt;T&gt; : DBObject&lt;T&gt; 
      where T : Contact&lt;T&gt;
  { 
      //field and methods ommitted...
  }
  
  public class Person : Contact&lt;Person&gt;
  { 
      //fields and methods ommitted...
  }
  
  public class Company : Contact&lt;Company&gt;
  {
      //fields and methods ommitted...
  }
  </code></pre>
  
  <p>The classes can then be used in such a manner:  </p>
  
  <pre><code>Person person = Person.Create(/* DataRow from a query result */);
  </code></pre>