title: microsoft moles, dynamically instrument
body: <p>Moles can be used in two ways:</p>
  
  <h3>Manually</h3>
  
  <ol>
  <li>Including [assembly: MoledType(typeof(_type_to_instrument))]</li>
  <li>Specify [HostType("Moles")]</li>
  <li>Call Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, object _receiver, MethodInfo method);</li>
  </ol>
  
  <h3>Dynamically</h3>
  
  <ol>
  <li>Add a {project name}.moles file: specifying assembly to mole. e.g.
  <code>&lt;Moles xmlns="http://schemas.microsoft.com/moles/2010/"&gt;
    &lt;Assembly Name="Samples.Moles"/&gt;
   &lt;/Moles&gt;</code></li>
  <li>Build and include reference to MolesAssemblies/{project_name}.Moles.dll</li>
  <li>Use M{class_name} auto-generated mole classes.</li>
  </ol>
  
  <p>What I noticed is that using the dynamic assembly doesn't require the test project to declare 'moled assemblies' attributes. This reduces the overhead, and a developer need only decorate each test method with the host type for moles; but further testing doesn't need to keep track of what types to instrument.</p>
  
  <p>Looking at the auto-generated code (using a disassembler) in the molesassemblies, it is easy to find that the needed instrumentation attributes. However, trying to write my own 'mole assembly', essentially replacing the autogenerated one, doesn't work and the runtime complains that my type needs to be instrumented. I am curious what I'm missing.</p>
  
  <p>I noticed that the autogenerated moles code declared the necessary MoledAssembly attributes. But in my tests, the test project seems to have to declare this property; it can't be declared by a referenced assembly to the project. However, in the case with the autogenerated assembly, it APPEARS the attribute <em>can</em> be declared "outside". This is my assumption based what I can see with disassembling the autogenerated moles dll; I cannot find any other difference. However, as I'm trying to explain, copying all the code (and attributes) from the disassembled autogenerated moles dll and building my own referenced assembly fails at runtime saying I have not marked the needed assembly-in-test to be instrumented (i.e. marked with the MoledAssembly) - tho it has, simply in my referenced asssembly.</p>
  
  <p>-- update</p>
  
  <p>At this point (likely due to my misunderstanding of what my code is missing) I feel we need to be very specific about what assembly has what. Let's say we have 4 dlls:</p>
  
  <ol>
  <li>Test.dll: The mstest project. Does not declared <code>MoledAssembly</code>.</li>
  <li>Moles.dll: The autogenerated dll created when using a <code>*.moles</code> file in your project. References the 4th dll, (see #4) <code>Sealed</code>. Declares <code>[assembly: MoledAssembly("Sealed")]</code>. Note that I'm trying to accomplish manual mole injection without this dll - it's only a conceptual reference or to be used in our discussion or troubleshooting.</li>
  <li>MyMoles.dll: My from-source compiled version of the autogenerated <code>Moles.dll</code>.</li>
  <li>Sealed.dll: Contains the code-under-test.</li>
  </ol>
  
  <p>In answers/comments/questions - let's refer to each part according to this list.</p>