Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 867747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:01:47+00:00 2026-05-15T10:01:47+00:00

Moles is an isolation framework created by Microsoft. A cool feature of Moles is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:01:47+00:00Added an answer on May 15, 2026 at 10:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Microsoft Moles to mock a method. This method call another method that
I have just see the Moles mocking framework from Microsoft Research, before I spend
Can Moles be used without having to use Pex? Also, Does anyone know when
My company is looking to standardize on an Isolation Framework. I was looking at
I'm trying to use Moles to mock the Socket class in System.Net.Sockets. I have
How can you verify interaction test using Moles. For example the following test is
I am having a problem getting a couple of the static methods in my
I am able to Mole WebRequest.CreateUri so that I can replace the hostname of
I'm working with the Moles Framework for mocking. I'm running into some roadblocks with
I was trying to find a consistent description on how to use moles isolation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.