c# - Calling subclass method after calling superclass method -
this question has answer here:
- chaining methods in base , derived class 3 answers
ultimately, call subclass method after calling superclass method. have code this:
public class superclass { protected wrapperclass<t> getwrapperclass<t>() { return new wrapperclass(somestuff).wrap<t>(); } public superclass commonmethod1() { // return this; } public superclass commonmethod2() { // else return this; } // many commonmethods } public class subclass : superclass { public wrapperclass<someclass> optionallastmethod1() { return base.getwrapperclass<someclass>(); } public wrapperclass<someotherclass> optionallastmethod2() { return base.getwrapperclass<someotherclass>(); } }
i this:
new subclass() .commonmethod1() .commonmethod2() // returns superclass .optionallastmethod1(); // this.
i avoid casting superclass subclass this:
var subclass = ((subclass)new subclass().commonmethod1().commonmethod2()).lastmethodtocall();
the important point here ease of use, not ease of implementation. input appreciated.
solved doing this. - removed irrelevant code.
public class superclass<s> s : superclass<s> { public s commonmethod1() { // return (s) this; } public s commonmethod2() { // else return (s) this; } // many commonmethods } public class subclass : superclass<subclass> { public wrapperclass<someclass> optionallastmethod1() { return base.getwrapperclass<someclass>(); } public wrapperclass<someotherclass> optionallastmethod2() { return base.getwrapperclass<someotherclass>(); } }
Comments
Post a Comment