在运行时动态地将代码添加到方法的开始/结束 [英] Adding code to the beginning / end of methods in runtime dynamically

查看:175
本文介绍了在运行时动态地将代码添加到方法的开始/结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道仪器是一种技术,可以将跟踪代码动态添加到方法中以启用跟踪和调试。



我想知道这是否只是一个跟踪选项,硬编码到CLR中只添加跟踪代码,还是有能力向方法添加任何代码?



例如,我想检查一个条件在每个单一方法的开始中调用某个类(对于权限来说)。我可以通过在执行时间的方法中添加动态代码来执行此操作吗?



我不知道这种跟踪工具的工作原理, m / p>

解决方案

基本上你应该做的是写一个CLR剖析器并使用c ++中的 profiler API

您需要实现ICorProfilerCallback接口。

您正在寻找的是在JITCompilationStarted回调。每次调用受管方法时,jit编译器将IL编译成机器代码之前,都会调用此方法。
运行期间的任何代码插入工作都应该在JITCompilationStarted中完成。

您可以查看开放源代码覆盖工具部分封面作为示例。


I know instrumentation is a technique to add trace code dynamically into the methods to enable tracing and debugging.

I was wondering if this is only a "Trace" option, hard coded into the CLR to add only trace code, or is there the ability to add any code to the methods?

For example, I want to check for a condition in the beginning of every single method call in a certain class (say for permissions). Can I do this via adding dynamic code to the beginning of the methods in execution time?

I'm not sure how this trace "instrumentation" thing works, but I'm wondering if this can be used for other goals too, or not.

解决方案

Basically what you should do is write a CLR profiler and use the profiler API in c++
You need to implement the ICorProfilerCallback interface.
What you are looking for is in the JITCompilationStarted callback. This method is called each time a managed method gets called and before the jit compiler compiles the IL into machine code. Any code insertion work during run time should be done in JITCompilationStarted.
You can look at the open source coverage tool part cover as an example how to do it.

这篇关于在运行时动态地将代码添加到方法的开始/结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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