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 1110589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:25:10+00:00 2026-05-17T02:25:10+00:00

I have a MEF based solution that has several exported implementations of an interface.

  • 0

I have a MEF based solution that has several exported implementations of an interface.

What I want to be able to do is have a switch that removes ALL the current parts associated with the interface, and in their place, replace them with just a single implementation. I’ve been trying to do this with a CompositionBatch object, but it doesn’t seem to work. Here’s an example of what I’m doing:

[Export(typeof(IFoo)]
public class Foo1 : IFoo
{ }

[Export(typeof(IFoo)]
public class Foo2 : IFoo
{ }

I then have my container:

var container = new CompositionContainer(....);

which will now contain parts representing Foo1 and Foo2. What I want to do, is replace them with another IFoo implementation. This is what I’m trying, and I thought that this would work:

var partsToRemove
   = from part in container.Catalog.Parts
       from exDef in part.ExportDefinitions
       where exDef.ContractName == AttributedModelServices.GetContractName(typeof(IFoo))
     select part.CreatePart();

var batch = new CompositionBatch(null, partsToRemove);

batch.AddPart(new Foo3());

container.Compose(batch);

I’m expecting container.Catalog.Parts to change in order to reflect my changes, but it doesn’t. It remains the same as when the container was first created.

What am I missing? Is this even the right approach? I’ve read Glenn Block’s CodeBetter article on using ExportProviders, but he mentions that he’ll write a part 2 in which he’ll look at implementing a filtering ExportProvider (which may be closer to what I need to do).

  • 1 1 Answer
  • 1 View
  • 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-17T02:25:11+00:00Added an answer on May 17, 2026 at 2:25 am

    So CompositionBatch is about adding and removing explicit object instances and is not connected to the catalog, which is about adding a set of definitions (aka types if you will) that are then later constructed into object instances in the CatalogExportProvider. To do what you want you will need to actually filter to catalog before you pass it to the container to exclude the types you want. (See http://mef.codeplex.com/wikipage?title=Filtering%20Catalogs for an example of a filtering catalog).

    Then if you want to add an explicit Foo instance you can use a CompositionBatch for that.

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

Sidebar

Related Questions

I have a MEF-based modular app that has started to fail to import classes.
I have an MVC application that is using MEF for the controllers, all working
I have a Prim class that implements an IPrimitiveDecomposer interface for MEF and inherits
I have a Prism v4 / MEF / WPF solution that loads module views
I am trying to get MEF to recompose all the parts that it knows
I am just getting into MEF and I have come across a problem that
It looks like that MEF framework creates objects which have default CTOR. How about
I have a unit test which depends on some code that uses MEF. When
I have the following problem with MEF: Interface definition to be used by host:
I have a Winforms desktop application that is loading multiple MEF parts with the

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.