How Moles Isolation framework is implemented?

Posted by Buu Nguyen on Stack Overflow See other posts from Stack Overflow or by Buu Nguyen
Published on 2010-06-11T09:52:00Z Indexed on 2010/06/11 10:12 UTC
Read the original article Hit count: 450

Filed under:
|
|
|

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);

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about mocking