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\
After poking around some other fluent APIs I found how to do it. It's not quite as clean, but it works well. Basically you introduce an intermediary base class for each derived type that you want to use and it passes the "TThis" type to the actual implementation.
public class FieldBase<T,TThis>
where TThis : FieldBase<T,TThis>
{
private string _name;
public TThis Name( string name )
{
_name = name;
return (TThis)this;
}
}
public class Field<T> : FieldBase<T,Field<T>>{}
public class SpecialFieldBase<T,TThis> : FieldBase<T,TThis>
where TThis : SpecialFieldBase<T,TThis>
{
public TThis Special(){ return (TThis)this; }
}
public class SpecialField<T> : SpecialFieldBase<T,SpecialField<T>>{}
// Yeah it works!
var specialField = new SpecialField<string>()
.Name( "bing" )
.Special();