title: How to replace an exported part / object in a MEF container?
body: <p>I have a WPF app running, which needs all operations that affect the UI to be on the UI Thread. WPF also provides a Dispatcher class that handles this - so I extracted that into a dependency.</p>
  
  <pre><code>public interface UIActionExecutor
      {
          void Do(Action action);
      }
  </code></pre>
  
  <p>So in my production code, I use an exported implementation which delegates to the WPF Dispatcher. I'm using MEF for DI.</p>
  
  <p>Now the problem, in my acceptance tests, I need to replace the part / object in the container that responds to <code>UIActionExecutor</code> by a Mock. So I need to remove <code>ExecutorUsingWpfDispatcher</code> from my container and add <code>MockUIActionExecutor</code> in its place. This sounds pretty simple (if I was not using MEF)... but my searching skills haven't helped me find an answer as to how to do this with the MEF container ? </p>
  
  <p><strong>Update:</strong>
  If anyone wants to know why/how the solution works - read <a href="http://codebetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx" rel="nofollow">Glenn Block's blog post#2</a>. This is what I ended up using</p>
  
  <pre><code>    var defaultExportProvider = new CatalogExportProvider(__defaultCatalog);
      var catalogOfMocks = new AssemblyCatalog(assemblyExportingMocks);
      // order of params important (precedence left to right)
      __container = new CompositionContainer(catalogOfMocks, defaultExportProvider);
      defaultExportProvider.SourceProvider = __container
  </code></pre>