14 November 2011

How Do I Detour a Mole Type Constructor?


Please be aware this information is based on Moles version v0.94.51023.0, 10/23/2010, and may not be accurate for prior versions.

Stub type constructors are not able to be detoured; because, they should be instantiated in the test, and have no need to be detoured.  Subsequently, no constructor detour appears in the stub type.

Constructors are located in the mole type root, and are named "Constructor", followed by the argument type names.  For example, the following are constructor detours for System.IO.Moles.MFile:

System.IO.Moles.MDirectoryInfo.ConstructorSerializationInfoStreamingContext
System.IO.Moles.MDirectoryInfo.ConstructorString
System.IO.Moles.MDirectoryInfo.ConstructorStringBoolean

These delegates may be set in the same manner as any other detour.  Detouring a constructor is especially helpful, when you want to redirect calls of one constructor to another, to enforce dependency injection or other settings.  For example:

MDemoClass.Constructor = instance => MDemoClass.ConstructorString("Hello, world!");

How To Detour a Static Constructor is discussed in a separate post.

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.