title: Generic 'TThis' for fluent classes
body: <p>I'm constructing a fluent interface where I have a base class that contains the bulk of the fluent logic, and a derived class that add some specialized behavior. The problem I'm facing is the return type of the fluent methods in the base class when called from an instance of the derived type. After invoking a method of the base class, only methods of the base class remain available for further fluent invocations.</p>
  <p>Changing the order in which the methods are invoked will help it to compile, but it makes it less readable  which is kinda the point for fluent interfaces. Is there a way to define some sort of &quot;This&quot; type for the the base class so that all methods return the same type.</p>
  <h3>Example</h3>
  <pre><code>public class Field&lt;T&gt;
  {
      public Field&lt;T&gt; Name( string name )
      {
          _name = name;
          return this;
      }
  }
  
  public SpecialField&lt;T&gt; : Field&lt;T&gt;
  {
      public SpecialField&lt;T&gt; Special(){ return this; }
  }
  
  
  // !!! Arrgh. Special is not a member of the Field&lt;T&gt; class.
  var specialField = new SpecialField()
      .Name( &quot;bing&quot; )
      .Special();
  </code></pre>
  <h3>Broken Solution</h3>
  <p>I've tried solving it by doing something like the following but it's not valid C# :( but at least expresses how I'd like to code the interface.</p>
  <pre><code>public class Field&lt;T,TThis&gt; : TThis
      where TThis : Field&lt;T,TThis&gt;
  {
      public TThis Name( string name ){...}
  }
  
  public SpecialField&lt;T&gt; : Field&lt;T,SpecialField&lt;T&gt;&gt;
  {
      public TThis Special(){ return this; }
  }
  </code></pre>