如何找到调用方法C#的全名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11367830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find FULL name of calling method C#
提问by Adam Schiavone
How can I find the full name of a calling method in c#. I have seen solutions:
如何在 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
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);
}
}
}
This simply outputs 'Main' How can I get it to print 'Sandbox.Program.Main'?
这只是输出 'Main' 我怎样才能让它打印 'Sandbox.Program.Main'?
Before anyone starts asking why I need to use this, its for a simple logging framework that I am working on.
在有人开始问我为什么需要使用它之前,它用于我正在开发的一个简单的日志记录框架。
EDIT
编辑
Adding onto Matzi's Answer:
添加到 Matzi 的答案中:
Here is the solution:
这是解决方案:
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);
}
}
}
Produces 'Sandbox.Program.Main' like it should
像它应该的那样产生“Sandbox.Program.Main”
采纳答案by Matzi
回答by burning_LEGION
in System.Reflection.MethodBasemethod GetCurrentMethodyou can find full information about callstack using classes etc
在System.Reflection.MethodBase方法中,GetCurrentMethod您可以使用类等找到有关调用堆栈的完整信息
回答by Zakaria
I think the best way to get the full name is:
我认为获得全名的最佳方法是:
this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
or try this
或者试试这个
string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name);
and if you want to display the most recent function call, you can use:
如果要显示最近的函数调用,可以使用:
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(0);
var methodName = sf.GetMethod();
, but if you want to display the tree of calling functions, you can do it like this:
,但如果你想显示调用函数的树,你可以这样做:
if (st.FrameCount >1)
{
// Display the highest-level function call
// in the trace.
StackFrame sf = st.GetFrame(st.FrameCount-1);
Console.WriteLine(" Original function call at top of call stack):");
Console.WriteLine(" {0}", sf.GetMethod());
}
for more info
回答by SubqueryCrunch
With this method you can reliably get the full name
使用此方法,您可以可靠地获得全名
public void HandleException(Exception ex, [CallerMemberName] string caller = "")
{
if (ex != null)
{
while (ex.InnerException != null)
ex = ex.InnerException;
foreach (var method in new StackTrace().GetFrames())
{
if (method.GetMethod().Name == caller)
{
caller = $"{method.GetMethod().ReflectedType.Name}.{caller}";
break;
}
}
Console.WriteLine($"Exception: {ex.Message} Caller: {caller}()");
}
}
回答by tom nobleman
current calling namespace which is not equal as the current namespace
当前调用命名空间与当前命名空间不相等
var mNamespace = new StackTrace().GetFrames()?.Select(x =>
{
try
{
return x.GetMethod().ReflectedType?.Namespace;
}
catch (Exception)
{
return string.Empty;
}
}).First(x => x != new StackTrace().GetFrame(0).GetMethod().ReflectedType?.Namespace);

