如何在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