如何痣隔离框架的实施?框架

2023-09-02 21:10:33 作者:爷单身1`却潇洒

痣是由微软创建的隔离框架。痣的一个很酷的功能是它可以模拟静态/非虚方法和密封类(这是不可能的框架,如MOQ)。下面是快速演示一下痣可以这样做:

Moles is an isolation framework created by Microsoft. A cool feature of Moles is that it can "mock" static/non-virtual methods and sealed classes (which is not possible with frameworks like Moq). Below is the quick demonstration of what Moles can do:

Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now);

// MDateTime is part of Moles; the below will "override" DateTime.Now's behavior
MDateTime.NowGet = () => new DateTime(2012, 1, 1); 
Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now);

看起来像痣是能够在运行时修改的东西CIL身体像 DateTime.Now 。由于痣是不是开源的,我很好奇,想知道哪些机构痣使用,以便修改方法CIL在运行时。任何人都可以摆脱任何轻?

Seems like Moles is able to modify the CIL body of things like DateTime.Now at runtime. Since Moles isn't open-source, I'm curious to know which mechanism Moles uses in order to modify methods' CIL at runtime. Can anyone shed any light?

推荐答案

痣实现一个CLR探查器(尤其是的 ICorProfilerCallback 接口),允许重写MSIL方法体之前,他们是由.NET运行时编译成汇编code。这是通过href="http://msdn.microsoft.com/en-us/library/ms230586.aspx"> JitCompileStarted 回调