Lambda expressions are anonymous methods. During the development period, we don't know what the method name is. So how can we call them recursively?
If you see meArticleIf you know who called your method, you should take a look at your mind and come up with ideas. That's right !! That is, you can directly use stackframe to obtain the method of the current execution, and then directly use invoke.
ExampleCode:
Class Test
{
/**/ /// <Summary>
///
/// </Summary>
/// <Param name = "V1"> </param>
/// <Param name = "V2"> </param>
/// <Param name = "num"> </param>
/// <Param name = "Total"> </param>
/// <Returns> </returns>
Public Delegate Long Addmethod ( Long V1, Long V2, Int Num, Int Cout );
/**/ /// <Summary>
///
/// </Summary>
/// <Param name = "addmethod"> </param>
/// <Param name = "cout"> </param>
Static Void Print (addmethod method, Int Cout)
{
Console. writeline (method (0,1, 2, Cout ));
}
Static Void Main ()
{
// Evaluate the number of similar data queues in 30th locations.
Print (V1, V2, num, cout) =>
{
Console. Write (V1 );
Console. Write ( " , " );
Num ++ ;
If (Num = Cout)
{< br> console. write (V2);
console. writeline ();
return V1 + V2;
}
Else
{
// Obtain the current method
Stackframe = New Stackframe ( 0 );
// Recursive call
Return ( Long ) (Frame. getmethod (). Invoke (Num, New Object [] {V2, (V1+V2), num, cout} ));
}
} , 30 );
Console. Read ();
}
}
The code is successfully run. The value of the successful code in the 30th position is: 514229.
At that time, there is another thing in. Net that can be easily referenced in the current call method, which is Methodbase The static method getcurrentmethod can directly obtain the reference of the currently executed method, so we can change the code of the two statements at stackframe to the following: // Recursive call
Return ( Long ) (Methodbase. getcurrentmethod (). Invoke (Num, New Object [] {V2, (V1+V2), num, cout} ));
The results after execution are the same (orz is not the same)