我如何得到一个的StackFrame执行对象? [英] How do I get the executing object for a stackframe?

查看:139
本文介绍了我如何得到一个的StackFrame执行对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用反射,能够使用System.Diagnostics.StackTrace获得调用堆栈(除了它可以是一个粗略的近似,由于JIT优化),并检查将StackFrame对象包含



我怎样才能在这堆栈帧的方法执行对象(此指针)的参考?



我知道我可以通过堆栈帧对象上调用实现getMethod()获取MethodBase,但我正在寻找的是沿的getObject线(东西)(which'd自然返回null如果该方法是静态的)。这似乎是堆栈帧对象只能用于查询静态地确定信息的方式信息,原产文件等这样的。



VS调试知道(尽管它可能使用另一种方法作者获得调用堆栈跟踪),作为一个可以双击调用堆栈窗口中的任意栈帧,并期待在当地人和类字段的值



编辑:
澄清:我想的对象的实例在其上调用的方法。即:如果方法foo()调用对象实例调用堆栈上的某处,并层叠到我做的堆栈跟踪的方法,我想获得我的地方执行堆栈跟踪到A的参考。 (不声明类型的方法基地)


解决方案

我敢肯定,这是不可能的。这里的原因:


  1. 这可能会破坏类型安全,因为任何人都可以查找一个框架,获取对象无论哪个

    AppDomain\螺纹他们正在执行或允许他们。


  2. 这个(C#)标识符真的只是一个参数实例方法(第一批),所以在现实中有静态方法和实例方法没有区别,编译器做它的魔力传递正确这个实例方法,当然这意味着你将需要访问所有方法参数,以获得这个对象。 (其中的StackFrame 不支持)




这可能是可以通过使用不安全代码来获得的第一个参数实例方法的指针,然后将其转换为正确的类型,但我不知道如何做到这一点的知识,只是一个想法。



BTW可以被编译后想象实例方法是像C#3.0的扩展方法,他们得到的这个指针作为第一个参数。


When using reflection it is possible to obtain the call stack (apart from that it can be a crude approximation due to JIT optimizations) using System.Diagnostics.StackTrace and examine the StackFrame objects contained.

How can I get a reference to the object (the this-pointer) on which a method in a stack frame is executing?

I know I can get the MethodBase by calling GetMethod() on the stack frame object, but what I'm looking for is something along the lines of GetObject() (which'd naturally return null if the method is static). It seems like the stack frame object can only be queried for statically determined info such as method info, originating file etc.

The VS debugger knows (although it probably use another method of obtaining the call stack trace), as one can double click any stack frame in the call stack window and look at the values of the locals and class fields.

EDIT: To clarify: I want the object instance on which the method was called. I.e.: If method Foo() is called on object instance A somewhere on the call stack, and it cascades to the method I do the stack trace, I'd like to obtain a reference to A from where I perform the stack trace. (Not the declaring type of the method base)

解决方案

I'm pretty sure that this is not possible. Here's why:

  1. This could break type safety, since anyone can lookup a frame, get the object regardless of which AppDomain\Thread they are executing on or permission they have.

  2. The 'this' (C#) identifier is really just an argument to the instance method (the first), so in reality there is no difference between static methods and instance methods, the compiler does its magic to pass the right this to an instance method, which of course means that you will need to have access to all method arguments to get the this object. (which StackFrame does not support)

It might be possible by using unsafe code to get the pointer of the first argument to an instance method and then casting it to the right type, but I have no knowledge of how to do that, just an idea.

BTW you can imagine instance methods after being compiled to be like C# 3.0 extension methods, they get the this pointer as their first argument.

这篇关于我如何得到一个的StackFrame执行对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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