简体   繁体   English

使C#方法“实现”委托

[英]Make a C# method “implement” a delegate

Does anyone know of a way in C# to force a method to "implement" a delegate? 有没有人知道C#中强制方法“实现”委托的方法?

Consider this greatly simplified example: (loosely based on a real world scenario I've encountered) 考虑这个大大简化的例子:(松散地基于我遇到的现实世界场景)

    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
    }

^ - Assume this logic cannot be called from ^^. ^ - 假设无法从^^调用此逻辑。

In this example, I'd essentially like to "force" methods to conform to the ComputationMethod signature in the same way an interface forces a class to implement certain methods. 在这个例子中,我基本上喜欢“强制”方法符合ComputationMethod签名,就像接口强制类实现某些方法一样。 Something equivalent to: 相当于:

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

Yes, obviously I can just copy and paste the method signature, but conceptually these implementations of ComputationMethod could be in an entirely different class without access to the source. 是的,显然我可以复制并粘贴方法签名,但从概念上讲,这些ComputationMethod的实现可能在一个完全不同的类中,而无法访问源。 Also, if someone then comes along and changes a method signature that is supposed to conform to a certain delegate, the source code will break, but it may break silently in an entirely different module. 此外,如果某人随后出现并更改了应该符合某个委托的方法签名,则源代码将会中断,但它可能会在完全不同的模块中静默破坏。

Thanks for any help. 谢谢你的帮助。

A delegate has a signature composed of the return type and the parameters (types and order) - if you have a method that matches that signature, it will match the delegate. 委托具有由返回类型和参数(类型和顺序)组成的签名 - 如果您有一个与该签名匹配的方法,它将匹配该委托。

That the methods you are asking about are static makes no difference. 你问的方法是static ,没有区别。

There is no direct way to ensure that any specific method will conform to a delegate signature - you could create interface with methods that conform to the signature and ensure it is used and implemented. 没有直接的方法来确保任何特定方法符合委托签名 - 您可以使用符合签名的方法创建接口并确保使用和实现它。

C# does not support this. C#不支持此功能。

However, you can simulate it by simply putting the method into a delegate: 但是,您可以通过简单地将方法放入委托来模拟它:

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

Changing the method or delegate signature would result in a compiler error one line above the method. 更改方法或委托签名将导致编译器错误超出方法一行。

(doing this with instance methods would require a constructor call) (使用实例方法执行此操作需要构造函数调用)

For added efficiency, you could do this in an unused nested class and/or in #if DEBUG . 为了提高效率,您可以在未使用的嵌套类和/或#if DEBUG

Either way, make sure to leave an explanatory comment. 无论哪种方式,请务必留下解释性评论。

If you do not require the use of delegates, you can use the following pattern. 如果您不需要使用委托,则可以使用以下模式。

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 change signature of the computation helper to: 并将计算助手的签名更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM