我可以通过C#中的堆栈跟踪获取方法的局部变量? [英] Can I get the method local variables through a stack trace in C#?

查看:362
本文介绍了我可以通过C#中的堆栈跟踪获取方法的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得我的堆栈跟踪详细的日志。我可以得到的StackFrame然后该方法,然后得到该方法的所有参数。正如下面的code:

I want to get a detailed log about my stack trace. I can get a StackFrame and then the method and then get all the parameters of that method. Just as the following code:

            StackTrace st = new StackTrace();
            StackFrame[] sfs = st.GetFrames();
            foreach (StackFrame sf in sfs)
            {
                MethodBase method = sf.GetMethod();
                ParameterInfo[] pis = method.GetParameters();
                foreach (ParameterInfo pi in pis)
                {
                      ....
                }
                Console.WriteLine(method.Name);
            }

但我怎么能得到的局部变量的方法内信息来源?

But how could I get the local variables infomation within a method?

有人可以提供一些线索对我?

Could someone shed some light on me?

非常感谢。

推荐答案

您可能要考虑的 LocalVariableInfo

示例FOM MSDN         //获取方法主体的信息。

Example fom MSDN // Get method body information.

MethodInfo mi = typeof(Example).GetMethod("MethodBodyExample");
MethodBody mb = mi.GetMethodBody();
Console.WriteLine("\r\nMethod: {0}", mi);

// Display the general information included in the 
// MethodBody object.
Console.WriteLine("    Local variables are initialized: {0}", 
    mb.InitLocals);

foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
    Console.WriteLine("Local variable: {0}", lvi);
}

这篇关于我可以通过C#中的堆栈跟踪获取方法的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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