2015-01-16 81 views
12

我想编写一个方法来完成一些工作,并最终返回与原始方法具有相同签名的另一个方法。这个想法是根据先前的字节值顺序处理一个字节流,而不进行递归。通过这样调用它:如何声明一个返回相同类型的Func委托的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委托。但我碰到了这个问题,这导致递归声明没有终止...

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

我不知何故在这里丢失。我怎么能解决这个问题?

回答

10

你可以简单的声明,当预定义Func<...>代表是不够的委托类型:

public delegate RecursiveFunc RecursiveFunc(byte input); 

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

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

的签名该方法虽然在编译时不知道。他不知道该参数是“Byte”。 – Servy

+4

@Servy在这种情况下...'代表RecursiveFunc RecursiveFunc (T输入)' –

+0

优秀 - 作品像魅力。在我的情况下,我知道这将是一个字节,但对于通用版本是+1。 – Marwie