I am trying to load an individual add-in using MEF. My scenario is as follows: when my application starts, it will search for a set of add-ins and load them into my app.
After this start-up phase, I would like the user to be able to load an add-in of their choosing. This is the bit I'm really struggling with.
My initial loading code is as follows:
var compositionContainer = new CompositionContainer();
var assemblyCatalog = new AssemblyCatalog(@"C:\MyPath\Experiment.dll");
.. catalogs for other addins..
var aggregateCatalog = new AggregateCatalog(assemblyCatalog);
CompositionContainer container = new CompositionContainer(aggregateCatalog);
container.ComposeParts(this);
foreach (var addin in addins)
{
addin.OnLoad(this);
}
I feel I should be able to add the new add-in to the catalog in the existing container, but I cannot find any way of adding it. The following does not work:
var assemblyCatalog = new AssemblyCatalog(filePath);
var catalog = this.compositionContainer.Catalog as AggregateCatalog;
catalog.Catalogs.Add(assemblyCatalog);
this.compositionContainer.ComposeParts(this);
How can I load the add-in that is in the dll at filePath. Furthermore, is there any way that I can call the .OnLoad method on that new add-in without calling it on the ones already loaded?