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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:14:27+00:00 2026-05-14T07:14:27+00:00

UPDATE As I’ve tried to get MEF working throughout my application, I’m coming across

  • 0

UPDATE

As I’ve tried to get MEF working throughout my application, I’m coming across more an more places where I just don’t get why it’s not automatically creating my library when I expect it to. I think it all comes back to what Reed was saying about needing MEF to create everything. So right now, I have an XML reader class that needs to use my CandySettings, but even though its ICandySettings property has the [Import] attribute, it doesn’t get imported. First I found out that [Import] doesn’t work on statics, so I changed this. But after that it still didn’t work. I think it’s because I manually create the XML reader object, and what MEF wants me to do instead is to [Import] the XML reader… which means that I now have to have an interface for that as well.

It’s almost like using IoC (or for MEF, at least), it’s an all-or-nothing affair. You can’t just arbitrarily use it here and there, because ultimately whatever class you want to inject properties into also needs to be created by MEF.

Please correct me if I am wrong!


Original post

Well, it’s not THAT bad yet. 🙂 But I do have questions after Reed has pointed me at MEF as a potential alternative to IoC (and so far it does look pretty good).

Consider the following model:
alt text http://bit.ly/9W0sHt

As you can see, I have an App, and this app uses Plugins (whoops, missed that association!). Both the App and Plugins require usage of an object of type CandySettings, which is found in yet another assembly.

I first tried to use the ComposeParts method in MEF, but the only way I could get this to work was to do something like this in the plugin code.

var container = new CompositionContainer();
container.ComposeParts(this, new CandySettings());

But this doesn’t make any sense, because why would I want to create the instance of CandySettings in the plugin? It should be in the App. But if I put it in the App code, then the Plugin doesn’t magically figure out how to get at ICandySettings, even though I am using [Import] in the plugin, and [Export] in CandySettings. EDIT (probably because I should be calling ComposeParts() from the App and then passing it the plugin?)

The way I did it was to use MEF’s DirectoryCatalog, because this allows the plugin, when constructed, to scan all of the assemblies in the current folder and automagically import everything that is marked with the [Import] attribute. So it looks like this, and potentially in every plugin:

var catalog = new DirectoryCatalog(".");
var container = new CompositionContainer(catalog);
container.ComposeParts(this);

This totally works great, but I can’t help but think that this is not how MEF was intended to be used?

  • 1 1 Answer
  • 0 Views
  • 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-14T07:14:28+00:00Added an answer on May 14, 2026 at 7:14 am

    The “trick” here is that you want to have MEF create your plugins for you.

    The way you’ll do this is to have your Application compose itself, with the Plugin types specified:

    class PluginRepository
    {
        [ImportMany(typeof(IPlugin))]
        IEnumerable<IPlugin> Plugins { get; set; }
    }
    

    If you do this, and have MEF Compose your “repository” class, MEF will construct the objects. It’ll then automatically Compose those as it constructs them, so ICandySettings will get composed without any intervention for you.

    You only need to manually “compose” an object if MEF isn’t constructing it for you.

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

Sidebar

Related Questions

UPDATE: It Is Now Working I was able to finally get it completed. A
Update: Solved, with code I got it working, see my answer below for the
Update: giving a much more thorough example. The first two solutions offered were right
Update : Looks like the query does not throw any timeout. The connection is
UPDATE : I updated the classes to look more like my actual model. When
UPDATE: Thanks for all your help guys! I just need to take a litte
UPDATE: I made a mistake in my debugging - this question is not relavent
[UPDATE] Was just an idiot mistake. See end for solution. I am trying to
UPDATE - I'm probably being daft, see my last update below. I just did
Update: It's not really necessary to remove added scripts considering once they run you

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.