回答“哪个方法叫我?" .NET在运行时?还是代码可以读取CallStack数据? [英] Answering "Which method called me?" at the run-time in .NET? Or is CallStack data readable by the code?

查看:79
本文介绍了回答“哪个方法叫我?" .NET在运行时?还是代码可以读取CallStack数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定存在methodA(),methodB()和methodC().

Presume that there are methodA() , methodB() and methodC().

然后在运行时调用methodC().

And methodC() is called at the run-time.

是否有可能知道methodC()是从什么方法调用的?

Is is possible to know methodC() is called from what method?

我在想,是否可以在运行时读取CallStack进行某些检查?如果可以,我认为应该没什么大不了的.

I was thinking if CallStack can be read at the run-time for some checks? If yes, I think it should not be a big deal.

有什么想法吗?

谢谢!

推荐答案

使用StackTraceStackFrame类.例如:

StackTrace stackTrace = new StackTrace();          
StackFrame[] stackFrames = stackTrace.GetFrames();

foreach (StackFrame stackFrame in stackFrames)
{
    string method = stackFrame.GetMethod().Name;
    // do some stuff with method
}

这篇关于回答“哪个方法叫我?" .NET在运行时?还是代码可以读取CallStack数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆