Using MethodInfo.GetCurrentMethod() in anonymous methods

前端 未结 3 1632
日久生厌
日久生厌 2021-01-18 02:38
public static void Main(string[] args)
{
    Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name);
    a();
}

This code will r

3条回答
  •  不要未来只要你来
    2021-01-18 02:57

    If you are looking for getting the name of the function in which the anonymous method resides in you could travel the stack and get the name of the calling method. Do note though, that this would only work as long as your desired method name is one step up in the hierarchy. Maybe there's a way of travelling up until you reach a non-anonymous method.

    For more information see: http://www.csharp-examples.net/reflection-calling-method-name/

提交回复
热议问题