让一个C#方法“实现”一个委托。

4

有没有办法在C#中强制一个方法“实现”一个委托?

考虑下面这个大大简化的例子:(基于我遇到过的真实场景)

    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's 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's signature to match ComputationMethod?
    private static int computeStandardDeviation(int termA, int termB, int termC, int termD)
    {
        //Implementation omitted
    }

^ - 假设这个逻辑不能从 ^^ 调用。

在这个例子中,我希望“强制”方法遵循 ComputationMethod 签名,就像接口强制类实现某些方法一样。相当于:

private static int computeAverage(int termA, int termB, int termC, int termD) : ComputationMethod
    {
        //Implementation omitted
    }

当然,我可以复制和粘贴方法签名,但是从概念上讲,这些ComputationMethod的实现可能完全在没有源代码访问权限的另一个类中。此外,如果有人更改了应符合某个委托的方法签名,源代码将会出错,但它可能会在完全不同的模块中默默地出错。

感谢您的任何帮助。

3个回答

5
C#不支持此功能。但是,您可以通过将方法放入委托来模拟它:
static readonly ComputationMethod _ForceCompliance = ComputeAverage;
private static int ComputeAverage(int termA, int termB, int termC, int termD) { ... }

修改方法或委托的签名会导致编译器在该方法上一行出现错误。

(对实例方法进行此操作需要调用构造函数)

为了更高的效率,您可以在未使用的嵌套类和/或 #if DEBUG 中执行此操作。

无论哪种方式,都要确保留下说明性注释。


4
一个委托具有由返回类型和参数(类型和顺序)组成的签名 - 如果你有一个匹配该签名的方法,它将与委托匹配。这些方法是 static 没有影响。
没有直接的方法来确保任何特定方法符合委托的签名 - 你可以创建带有符合该签名的方法的接口,并确保其被使用和实现。

0
如果您不需要使用委托,可以使用以下模式。
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.
    }
}

并将计算辅助函数的签名更改为:

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接