I have some classes that i'd like chain methods of to provide a fluent style of config.
e.g.
public class BaseFoo
{
private bool _myProp = false;
public [[[BaseFoo?]]] SetMyProperty()
{
this._myProp = true;
return this;
}
}
public class DerivedFoo : BaseFoo
{
public bool _derivedProp = false;
public DerivedFoo SetDerivedPropery()
{
this._derivedProp = true;
return this;
}
}
The problem is clearly when trying to chain these together, as soon as I use the base method the returned type of BaseFoo. Obviously I could cast this back to DerivedFoo, but is there a simply way of returning an object of the derived class type.
The only way I can think of doing it is chaining constructors together and passing in the parent type to the initial constructor but usure of syntax.
Another way would be to have like proxy methods for each of the subclass', but returning the derived type.
DerivedFoo foo = new DerivedFoo();
foo.SetMyProperty().SetDerivedPropery(); // wont work because the SetMyProperty returns type of BaseFoo
foo.SetDerivedPropery().SetMyProperty(); // works because I know i'm calling the method of the derived class first
(foo.SetMyProperty() as DerivedFoo).SetDerivedPropery(); // works as i'm casting result of base method to derived type
any ideas?
Thanks in advance,
Sam