c# - Is it possible to have two functions with the same signature called under different contexts? (Attribute manipulation) -
i've come across interesting problem, result of design of 3rd party api have no control over.
consider following example code:
class myclass : thirdpartyclass { void specialfunction() { //... } } the 3rd party api calls specialfunction after internal processing.
i can add attribute tell api should called prior internal processing:
[dofirst] void specialfunction() { ... } however means have have 2 additional classes if want run same function before , after api's internal processing:
class myclass : thirdpartyclass { public void dospecialfunction() { ... } } class myclasspre : thirdpartyclass { myclass myclass; [dofirst] void specialfunction() { myclass.dospecialfunction(); } } class myclasspost : thirdpartyclass { myclass myclass; void specialfunction() { myclass.dospecialfunction(); } } this not valid:
class myclass : thirdpartyclass { [dofirst] void specialfunction() { _specialfunction(); } void specialfunction() { _specialfunction(); } void _specialfunction() { ... } } is there perhaps c# way can manipulate attributes, or function signatures, additional classes aren't required?
perhaps via way of extension? (specialfunction not override)
as mentioned in comments, cannot have 2 methods same name , arguments differ decorated attributes.
presumably third party api using reflection identify specialfunction() , should able simplify workaround follows:
// instance of class after public class myclass : thirdpartyclass { public virtual void specialfunction() { // work goes here } } // instance of class first public class myclasspre : myclass { [dofirst] public override void specialfunction() { base.specialfunction(); } } if third party api provided organisation partnered or whom consuming services, may able request add [doafter] attribute triggers default behaviour well. in case decorate follows:
class myclass : thirdpartyclass { [dofirst] [doafter] void specialfunction() { // ... } }
Comments
Post a Comment