如何在C#中查找调用方法的全名 [英] How to find the FULL name of the calling method in C#
本文介绍了如何在C#中查找调用方法的全名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#中找到调用方法的全名?我看到了解决方法:
How can I find the full name of a calling method in C#? I have seen solutions:
但是他们只会给我最高水平.考虑示例:
But they only give me the top level. Consider the example:
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);
}
}
}
这只是输出"Main".如何获取它以打印"Sandbox.Program.Main"?
This simply outputs 'Main'. How can I get it to print 'Sandbox.Program.Main'?
这是我正在使用的简单日志记录框架.
It's for a simple logging framework that I am working on.
添加到Matzi的答案中
Adding onto Matzi's Answer:
这是解决方案:
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".
It produces 'Sandbox.Program.Main' like it should.
推荐答案
这类似于
这篇关于如何在C#中查找调用方法的全名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文