Does anyone know a way in C # to force a method to "implement" a delegate?
Consider this very simplified example: (weakly based on the real-world scenario I came across)
private delegate int ComputationMethod(int termA, int termB, int termC, int termD); private int computationHelper(int termA, int termB, int termC, int termD, ComputationMethod computationMethod) { //Some common logic^ int answer = computationMethod(termA, termB, termC, termD); //Some more common logic^ return answer; } public int ComputeAverage(int termA, int termB, int termC, int termD) { //^^ return computationHelper(termA, termB, termC, termD, computeAverage); } public int ComputeStandardDeviation(int termA, int termB, int termC, int termD) { //^^ return computationHelper(termA, termB, termC, termD, computeStandardDeviation); } //Is there some way to force this method signature to match ComputationMethod? private static int computeAverage(int termA, int termB, int termC, int termD) { //Implementation omitted } //Is there some way to force this method signature to match ComputationMethod? private static int computeStandardDeviation(int termA, int termB, int termC, int termD) { //Implementation omitted }
^ - Suppose that this logic cannot be called from ^^.
In this example, I would essentially want the methods to βforceβ the ComputationMethod signature in the same way that the interface forces the class to implement certain methods. Something equivalent:
private static int computeAverage(int termA, int termB, int termC, int termD) : ComputationMethod {
Yes, I can simply copy and paste the method signature, but conceptually these ComputationMethod implementations can be in a completely different class without access to the source. In addition, if someone then comes and changes the method signature, which must correspond to a specific delegate, the source code is interrupted, but it may break in a completely different module.
Thanks for any help.