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 的博客