c# - 如何声明返回相同类型的 Func Delegate 的 Func Delegate?

标签 c# delegates func

我想编写一个方法来完成一些工作并最终返回另一个与原始方法具有相同签名的方法。这个想法是根据前一个字节值顺序处理字节流,而不用递归。通过这样调用它:

MyDelegate executeMethod = handleFirstByte //What form should be MyDelegate?

foreach (Byte myByte in Bytes)
{
    executeMethod = executeMethod(myByte); //does stuff on byte and returns the method to handle the following byte
}

为了移交方法,我想将它们分配给 Func 委托(delegate)。但是我遇到了一个问题,这导致递归声明没有终止......

Func<byte, Func<byte, <Func<byte, etc... >>>

我在这里迷路了。我该如何解决这个问题?

最佳答案

您可以在预定义的 Func<...> 时简单地声明一个委托(delegate)类型委托(delegate)不够:

public delegate RecursiveFunc RecursiveFunc(byte input);

如果你需要它,你也可以使用泛型:

public delegate RecursiveFunc<T> RecursiveFunc<T>(T input);

关于c# - 如何声明返回相同类型的 Func Delegate 的 Func Delegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27989296/

相关文章:

c# - 如何传入具有泛型类型参数的函数?

swift - 将多个 pickerView 选定值导出到 UILabel 上的数字

go - 在新包中使用标志值

c# - 如何检测和避免无限循环?

c# - Delegate.CreateDelegate 与 DynamicMethod 与表达式

c# - 事件和委托(delegate)困境

ios - 如何让我的倒数计时器在每次加载 View 时重置?

c# - 不能在 ContractClass 中使用 ContractAbbreviator?

c# - kinect色框里面的按钮

c# - 为什么不调用 WebBrowser 控件中通过 setTimeout 方法调度的代码