Mar*_*wie 12 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
}
Run Code Online (Sandbox Code Playgroud)
要切换方法,我想将它们分配给Func委托.但我遇到的问题是,这导致递归声明而没有终止...
Func<byte, Func<byte, <Func<byte, etc... >>>
Run Code Online (Sandbox Code Playgroud)
我不知何故在这里迷路了.我怎么能绕过那个?
Luc*_*ski 10
当预定义的Func<...>委托不足时,您可以简单地声明委托类型:
public delegate RecursiveFunc RecursiveFunc(byte input);
Run Code Online (Sandbox Code Playgroud)
如果您需要它,您也可以使用泛型:
public delegate RecursiveFunc<T> RecursiveFunc<T>(T input);
Run Code Online (Sandbox Code Playgroud)