我想获得有关我的堆栈跟踪的详细日志。我可以获得一个StackFrame,然后获取该方法,然后获取该方法的所有参数。正如以下代码:
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);
}
但是如何才能在方法中获得局部变量信息?
有人可以对我有所了解吗?
非常感谢。
答案 0 :(得分:3)
您可能需要查看LocalVariableInfo。
MSDN示例 //获取方法正文信息。
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);
}