How Moles Isolation framework is implemented?

大憨熊 提交于 2019-11-27 11:49:49

Moles implements a CLR profiler (in particular the ICorProfilerCallback interface) that allows to rewrite MSIL method bodies before they are compiled into assembly code by the .NET runtime. This is done in particular through the JitCompileStarted callback.

In each method, Moles introduces a detour that looks like this:

static struct DateTime 
{
    static DateTime Now
    {
        get 
        {
            Func<DateTime> d = __Detours.GetDelegate(
                null, // this point null in static methods
                methodof(here) // current method token
                );
            if(d != null)
                return d();
            ... // original body
        }
    }
}

When you set a mole, your delegate is stored in the underlying __Detours dictionary which gets looked up whenver the method is executed.

This is working like as wrapper to any assembly you want, for example mscorlib (this example baseing on Moles Assembly Wrapper of mscorlib). This giving to you power to replace any .NET method by the delegate written by coder.

This is not working automagicaly. You must first create before this start works, Moles XML configuration file with list of Assemblies to "Wrapper" and by this code Moles generate a References of this assembiles from config file. And you must in this file add using namespace System.Moles, and before function [HostType("Moles")]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!