获取到某一方法的调用者的类名、方法名、命名空间(转)

1、返回当前方法所在的类名:

using System.Reflection;
string className = MethodBase.GetCurrentMethod().ReflectedType.Name;

2、返回调用当前方法的方法名:

using System.Diagnostics;
using System.Reflection;

StackTrace   trace   =   new   StackTrace();   
MethodBase   methodName =  trace.GetFrame(1).GetMethod();
3、例子
    在Program类Main方法中调用TestCodon.Test方法
    class Program
    {
        static void Main(string[] args)
        {

            TestCodon _tt = new TestCodon();
            _tt.Test();
 
            Console.ReadKey();
        }
    }
    public class TestCodon : AbstractCodon
    {
        public void Test()
        {
            StackTrace trace = new StackTrace();
            MethodBase methodName = trace.GetFrame(1).GetMethod();
            
            Console.WriteLine(methodName.DeclaringType.FullName+"."+methodName.Name);
        }
     }

转自 Kevin 的博客

原文地址:https://www.cnblogs.com/mrhgw/p/2053035.html