如何获得所有局部变量的转储? [英] How to get a dump of all local variables?
问题描述
我怎样才能得到所有本地和放大器的转储;当发生异常时会话变量?我的想法写某种基于反射的功能,将询问主叫功能和安培的;创建变量和放大器转储;值。
How can I get a dump of all local & session variables when an exception occurs? I was thinking of writing some sort of reflection based function that would interrogate the calling function & create a dump of variables & values.
有没有办法,我可以使用现有的库?
Is there an existing library that I can use?
更新
说给同事后,我指着AOP或面向方面的编程。以下是我的理解...使用AOP,一会简单的装修方法和放大器;班,某些属性。 AOP框架,然后注入code或围绕这些类和放大器;方法。有两种不同的类型的框架中,一个注入code&放;然后编译程序集和放大器;第二只需使用反射&放大器;陷阱你装饰了通话和包装无论code周围的方法在运行时。
After speaking to a colleague, I was pointed to AOP or Aspect Oriented Programming. Here is what I understand ... Using AOP, one would simple decorate the methods & classes with certain attributes. AOP framework then injects code in or around these classes & methods. There are two separate kinds of framework, one that injects code & then compiles the assembly & the second simply uses reflection & traps the call which you have decorated and wraps whatever code around the method at runtime.
我希望所有有意义。我会做这个和放更多的研究;发布我的方法。
I hope all that makes sense. I will be doing more research on this & post my approach.
感谢球员...
推荐答案
我不知道如果这是你在找什么。但是,如果你在一个catch块是你可以通过以下方式各个领域和这个类的属性:
I'm not sure if this is what you're looking for. But if you're in a catch-block you can get all fields and properties of this class in the following way:
try
{
double d = 1 / 0;
}
catch (Exception ex)
{
var trace = new System.Diagnostics.StackTrace();
var frame = trace.GetFrame(1);
var methodName = frame.GetMethod().Name;
var properties = this.GetType().GetProperties();
var fields = this.GetType().GetFields(); // public fields
// for example:
foreach (var prop in properties)
{
var value = prop.GetValue(this, null);
}
foreach (var field in fields)
{
var value = field.GetValue(this);
}
foreach (string key in Session)
{
var value = Session[key];
}
}
我已经展示了如何获取在异常发生只为完整起见方法名。
I've showed how to get the method name where the exception occured only for the sake of completeness.
- Type.GetProperties Method
- Type.GetFields Method
- PropertyInfo.GetValue Method
- FieldInfo.GetValue Method
- StackTrace Class
使用的BindingFlags 你可以指定约束条件,比如你只想要从这个类的属性,而不是继承:
With BindingFlags you can specify constraints, for example that you only want properties of this class and not from inherited:
<一个href=\"http://stackoverflow.com/questions/1544979/net-reflection-getproperties-with-bindingflags-declaredonly\">.NET反思:的GetProperties()与BindingFlags.DeclaredOnly
当然,上面应该给你只是一个起点,如何做手工,你应该封装所有成类。所以这是未经考验的我从来没有用它自己。
Of course the above should give you only a starting-point how to do it manually and you should encapsulate all into classes. I've never used it myself so it's untested.
这篇关于如何获得所有局部变量的转储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!