怎样声明函数求代表返回相同类型的函数求代表? [英] How do I declare a Func Delegate which returns a Func Delegate of the same type?
问题描述
我喜欢写,做一些工作,最后返回具有相同签名与原始方法的另一种方法的方法。我们的想法是处理取决于前一字节值顺序的字节流,而不进入一个递归。通过调用它是这样的:
I'd like to write a method which does some work and finally returns another method with the same signature as the original method. The idea is to handle a stream of bytes depending on the previous byte value sequentially without going into a recursion. By calling it like this:
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
}
要交接方法我想将它们分配给函数求委托。但是,我跑进这会导致一个递归声明没有终止...
To handover the method I want to assign them to a Func delegate. But I ran into the problem that this results in a recursive declaration without termination...
Func<byte, Func<byte, <Func<byte, etc... >>>
我莫名其妙地丢在这里。 ?我怎么能解决这个让
I'm somehow lost here. How could I get around that?
推荐答案
您可以简单地声明一个委托类型时,预定义的 Func键< ...>
代表是不够的:
You can simply declare a delegate type when the predefined Func<...>
delegates aren't sufficient:
public delegate RecursiveFunc RecursiveFunc(byte input);
而如果你需要它,你可以使用泛型太:
And in case you need it, you can use generics too:
public delegate RecursiveFunc<T> RecursiveFunc<T>(T input);
这篇关于怎样声明函数求代表返回相同类型的函数求代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!