I've come across an interesting problem, probably as a result of the design of a 3rd party API I have no control over.
Consider the following example code:
class MyClass : ThirdPartyClass {
void SpecialFunction() {
//...
}
}
The 3rd party API calls SpecialFunction
after internal processing.
I can add an attribute to tell the API that it should be called prior to internal processing:
[DoFirst]
void SpecialFunction() { ... }
However this means that I have to have two additional classes if I want to run the same function before and after the 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 is obviously not valid:
class MyClass : ThirdPartyClass {
[DoFirst]
void SpecialFunction() { _SpecialFunction(); }
void SpecialFunction() { _SpecialFunction(); }
void _SpecialFunction() { ... }
}
Is there perhaps some C# way I can manipulate the attributes, or the function signatures, so that additional classes aren't required?
Perhaps via some way of extension? (SpecialFunction
is not an override)