How to find FULL name of calling method C#

前端 未结 5 1955
别那么骄傲
别那么骄傲 2020-12-31 00:24

How can I find the full name of a calling method in c#. I have seen solutions:

How I can get the calling methods in C#

How can I find the method that called

5条回答
  •  遥遥无期
    2020-12-31 00:56

    With this method you can reliably get the full name

        public void HandleException(Exception ex, [CallerMemberName] string caller = "")
        {
            if (ex != null)
            {
                while (ex.InnerException != null)
                    ex = ex.InnerException;
    
                foreach (var method in new StackTrace().GetFrames())
                {
                    if (method.GetMethod().Name == caller)
                    {
                        caller = $"{method.GetMethod().ReflectedType.Name}.{caller}";
                        break;
                    }
                }
    
                Console.WriteLine($"Exception: {ex.Message} Caller: {caller}()");
            }
        }
    

提交回复
热议问题