登录C#时,如何学习调用当前方法的方法的名称?我知道所有这些System.Reflection.MethodBase.GetCurrentMethod(),但我想在堆栈跟踪中向下迈出一步.我考虑过解析堆栈跟踪,但我希望找到一种更清晰,更明确的方法,比如Assembly.GetCallingAssembly()方法.
假设有methodA(),methodB()和methodC().
并且在运行时调用methodC().
有可能知道从哪个方法调用methodC()?
我在想是否可以在运行时读取CallStack以进行某些检查?如果是,我认为这应该不是什么大问题.
有任何想法吗?
谢谢!
如何在运行时从方法中找到调用方法的方法名称?
例如:
Class A
{
M1()
{
B.M2();
}
}
class B
{
public static M2()
{
// I need some code here to find out the name of the method that
// called this method, preferably the name of the declared type
// of the calling method also.
}
}
Run Code Online (Sandbox Code Playgroud) .net ×2
reflection ×2
c# ×1
call ×1
callstack ×1
logging ×1
methods ×1
runtime ×1
stack-trace ×1