Moles是由微软创建的一种隔离框架。 Moles的一个很酷的特性是它可以“模拟”静态/非虚方法和密封类(这在像Moq这样的框架中是不可能的)。以下是Moles可以做到的快速演示:
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);
似乎 Moles 能在运行时修改像 DateTime.Now
这样的方法的 CIL 体。由于 Moles 不是开源的,我想知道它使用哪种机制来在运行时修改方法的 CIL。有人能够详细解释一下吗?