如何从ObjectID获取托管对象 [英] How to get managed object from ObjectID

查看:60
本文介绍了如何从ObjectID获取托管对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在为一些自定义要求构建一个.Net Profiler,我需要捕获异常细节,即使它在代码中得到了正确处理。为此 -

I am building a .Net Profiler for some custom requirement where I need to capture the exception details even though it got handled properly in the code. To do so-



  • 我实现了ICorProfilerCallback

  • 用于COR_PRF_MONITOR_EXCEPTIONS的SetEventsMask

  • 实现了ExceptionThrown回调


到目前为止,我正在为每个抛出的异常收到回调。但是,它给出了OjbectID,它是指向实际异常对象的指针。我想要更多详细信息,如消息,调用关于异常的堆栈等。

So far so good, I am getting a callback for every exception being thrown. However, it gives OjbectID that is a pointer to the actual exception object. I want more details like the message, call stack, etc. about the exception.


如何从ObjectID获取对象详细信息?

How do I get object details from ObjectID?

Hitesh

推荐答案

我担心你的运气不好。如果属性只是包装字段,那么你可以使用

GetClassLayout
找到可以读取字段值的偏移量(尽管这可能是如果字段不在外部可见则很脆弱。)不幸的是,StackTrace和Message属性都涉及计算;更糟糕的是,Message
属性是虚拟的并被某些异常类型覆盖。

I'm afraid your out of luck.  If the properties were simply wrapping fields, then you could use GetClassLayout to find the offset where you can read the field value (though this would be fragile if the field is not externally visible). Unfortunately, both the StackTrace and Message properties involve calculations; and to make things worse, the Message property is virtual and overridden by some exception types.

如果你真的需要消息和堆栈跟踪,那么你需要在有权访问异常的地方注入IL来调用属性访问器方法,并使用像pinvoke或calli操作码之类的东西将值传递给你的探查器。 (你不能用
来电话他在你的回调中的方法)

If you really need the message and stack trace, then you will need to inject IL somewhere that has access to the exception to call the property accessor methods and pass the values to your profiler using something like pinvoke or a calli opcode. (you can't call the methods from within your callback)


这篇关于如何从ObjectID获取托管对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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