有没有办法在c#中获得对调用对象的引用? [英] Is there any way to get a reference to the calling object in c#?

查看:12
本文介绍了有没有办法在c#中获得对调用对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能(例如)遍历堆栈帧,检查每个调用对象是否与接口匹配,如果匹配,则从中提取一些数据.

What I'm wondering is if it's possible to (for instance) to walk up the stack frames, checking each calling object to see if matches an interface, and if so extract some data from it.

是的,我知道这是不好的做法,我想知道是否有可能.

Yes, I know it's bad practice, I'm wondering if it's possible.

推荐答案

不,没有 - 至少在不使用某种描述的分析/调试 API 的情况下不会.您可以遍历堆栈以找到调用方法,但需要注意的是,由于 JIT 优化,它确实很慢并且可能不准确.这不会告诉你调用的对象是什么(如果确实有一个).

No, there isn't - at least not without using a profiling/debugging API of some description. You can walk the stack to find the calling method, with the caveat that it's really slow and may be inaccurate due to JIT optimisations. That won't tell you what the calling object is though (if indeed there is one).

这篇关于有没有办法在c#中获得对调用对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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