How do I declare a Func Delegate which returns a Func Delegate of the same type?

隐身守侯 提交于 2020-01-02 01:00:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/27989296/how-do-i-declare-a-func-delegate-which-returns-a-func-delegate-of-the-same-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!