我怎么能找到调用当前方法的方法? [英] How can I find the method that called the current method?
本文介绍了我怎么能找到调用当前方法的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当在C#中记录,我怎么可以了解调用当前方法的方法的名称?我知道所有关于 System.Reflection.MethodBase.GetCurrentMethod()
,但我想在堆栈跟踪中去一步在这下面。我考虑过解析的堆栈跟踪,但我希望能找到一个更清洁更明确的方式,像 Assembly.GetCallingAssembly()
而是方法。
When logging in C#, how can I learn the name of the method that called the current method? I know all about System.Reflection.MethodBase.GetCurrentMethod()
, but I want to go one step beneath this in the stack trace. I've considered parsing the stack trace, but I am hoping to find a cleaner more explicit way, something like Assembly.GetCallingAssembly()
but for methods.
推荐答案
试试这个:
using System.Diagnostics;
// Get call stack
StackTrace stackTrace = new StackTrace();
// Get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
这是从的 获取调用方法使用反射[C#] 。
这篇关于我怎么能找到调用当前方法的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文