locked
Is it possible to add MEFComponent for Visual Studio extension at runtime? RRS feed

  • Question

  • I am thinking if its possible to load MEF components' assemblies during runtime instead of specifying in VSIXmanifest's asset.

    I have to load different MEFComponents which would be read by my base extension and execute them. For instance, I would like to extend the VS Editor but the extension should work according to the dlls that is put in a predefined directory within a solution.

    Thanks!

    Monday, November 18, 2013 6:20 PM

Answers

  • No, that would require dynamic recomposition of the entire MEF graph, which is a very expensive thing to do.
    Monday, November 18, 2013 6:35 PM

All replies

  • No, that would require dynamic recomposition of the entire MEF graph, which is a very expensive thing to do.
    Monday, November 18, 2013 6:35 PM
  • Thank you for your answer. However, I don't understand why you'd need a re composition of MEF graph. I am loading MEF component from assembly using the following code:

      var directoryCatalog = new DirectoryCatalog(directoryPath);                 var container = new CompositionContainer(directoryCatalog);                 var batch = new CompositionBatch();

                    batch.AddPart(this);                 container.Compose(batch);

    Now when I put breakpoint after composing the container, I can see that the MEF picks up all the import and export parts correctly. So I know MEF recognizes the parts, but the extension "plugin" still does not work. Is there anyway to check if the all imports/exports are being satisfied instead? 


    Monday, November 18, 2013 6:52 PM
  • It isn't clear what you mean. You are spinning up your own MEF catalog that VS knows nothing about? Then, yes, that should be supported, but nothing you put into that catalog would be seen by VS because:

    1: It knows nothing about that catalog's existence

    2: Even if it did it would need to recompose its entire graph since that catalog could contain items that either make rejected parts valid or make accepted parts invalid.

    How is the 'extension plugin' expected to be initialized by MEF? Generally MEF can only auto-satisfy on things that MEF creates, so if you say new <anything>() MEF has no oppurtunity to satisfy any [Imports] in that thing you just newed up. You would need to run SatisfyImportsOnce on it I believe. If you ask the container for the part(s) explicitly after composition it should give them to you.
    Monday, November 18, 2013 8:45 PM
  • I think you're right; VS doesn't know the catalog that I am creating. I was probably looking in the wrong direction. 

    So, rephrasing the question, is there anyway to add the parts that I have defined to the VS catalog? (Should I create a new thread asking this question?)

    Thank you for your time.

    Monday, November 18, 2013 9:12 PM
  • No, as I said, doing so, in general, would cause us to have to recompose the entire MEF composition graph. Pieces you added could have made a previously rejected part now acceptable, that, in turn, could now make another rejected part acceptable, or perhaps a previously accepted part now rejected, that kind of thing ripples through the entire graph.

    Recomposing the MEF graph on a scale as large as VS isn't feasible with anything approaching reasonable performance.

    Your best bet is to somehow proxy up items to MEF via your VSIXManifest and those things themselves have a further extensibility model that VS knows nothing about that you delegate to when your VS consumed MEF bits get called on.


    Monday, November 18, 2013 9:53 PM
  • Thank you. I have marked your first reply as answer.
    Tuesday, November 19, 2013 5:18 PM