15 December 2011

How To Mole the System Assembly

The System namespace may be moled, in addition to any other child namespace.  For instance, you may want to detour calls away from System.StringComparer, which is a member of the System assembly.   To mole members of the .NET Framework "System" namespace, follow these four steps:

  1. Right-click the References node of your test class -- the context menu appears
  2. In the context menu, click "Add Moles Assembly for mscorlib"
  3. Build the project/solution
  4. Add a using declaration for the System.Moles assembly:
    using System.Moles;
    
Your test project is now ready to detour members of the .NET Framework System assembly.  To follow the example, above, the StringComparer method may be detoured, using the following code.  (This particular detour always returns 0, which indicates both string representations of the objects are equal.)

System.Moles.MStringComparer.AllInstances.CompareObjectObject = (instance, lhs, rhs) => 0;

No comments:

Post a Comment

Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.