如何在c#中查找调用方法的全名。我已经看到了解决方案:如何查找调用方法C的完整名称#
How I can get the calling methods in C#
How can I find the method that called the current method?
Get Calling function name from Called function
但他们只给我的最高水平。请看例子:
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
test();
}
static void test()
{
var stackTrace = new StackTrace();
var methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name);
}
}
}
这只是输出“主”我怎样才能得到它打印“Sandbox.Program.Main”?
在任何人开始问为什么我需要使用它,它为我正在处理一个简单的日志记录框架。
编辑
添加到Matzi的答案:
这里是解决方案:
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
test();
}
static void test()
{
var stackTrace = new StackTrace();
var methodBase = stackTrace.GetFrame(1).GetMethod();
var Class = methodBase.ReflectedType;
var Namespace = Class.Namespace; //Added finding the namespace
Console.WriteLine(Namespace + "." + Class.Name + "." + methodBase.Name);
}
}
}
产生了 'Sandbox.Program.Main' 像它应该
可能重复[使用的System.Reflection获取一个方法的全名(http://stackoverflow.com/questions/2968352/using-system-reflection-to-get-a-methods-full -name) – user7116 2012-07-06 18:39:32