Make the C # method "implement" the delegate

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 { //Implementation omitted } 

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.

+4
source share
3 answers

C # does not support this.

However, you can mimic it by simply placing the method in the delegate:

 static readonly ComputationMethod _ForceCompliance = ComputeAverage; private static int ComputeAverage(int termA, int termB, int termC, int termD) { ... } 

Changing a method or delegating a signature will result in a compiler error on one line above the method.

(to do this using instance methods, you need to call the constructor)

For efficiency, you can do this in an unused nested class and / or in #if DEBUG .

In any case, do not forget to leave an explanatory comment.

+4
source

The delegate has a signature consisting of the return type and parameters (types and order) - if you have a method that matches this signature, it will correspond to the delegate.

That the methods you are asking about is static doesn't matter.

There is no direct way to guarantee that a particular method will match the delegate’s subscription - you can create an interface with methods that match the signature and make sure that it is used and implemented.

+4
source

If you do not need to use delegates, you can use the following template.

 public interface IComputationMethod { int ComputationMethod(int termA, int termB, int termC, int termD); } public class AverageComputer : IComputationMethod { public override int ComputationMethod(int termA, int termB, int termC, int termD) { // omitted. } } public class StDevComputer : IComputationMethod { public override int ComputationMethod(int termA, int termB, int termC, int termD) { // omitted. } } 

And replace the signature of the calculation assistant with:

 private int computationHelper(int termA, int termB, int termC, int termD, IComputationMethod computation) { //Some common logic^ int answer = computation.ComputationMethod(termA, termB, termC, termD); //Some more common logic^ return answer; } 
0
source

Source: https://habr.com/ru/post/1401865/


All Articles