How to create a directory that will look for subdirectories for plugins

So I have a directory of directories shown below:

DirectoryCatalog directoryCatalog = new DirectoryCatalog(@".\Plugins");
var catalog = new AggregateCatalog(directoryCatalog);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);

      

Which will find a .dll in my folder Plugins

that matches my import criteria. Now, to prevent link collisions in my plugins, I would like to put each plugin in a separate folder.

eg.

Plugins -> Plugin1 -> plugin1.dll

      -> Plugin2 -> plugin2.dll

      

But mine DirectoryCatalog

doesn't find these plugins. Is it possible to implement this or my plugin libraries should be in the specified folder.\Plugins

+3


source to share


2 answers


You can solve the problem by adding more properties DirectoryCatalogs

to AggregateCatalog.Catalogs

the following:



var catalog = new AggregateCatalog();
// iterate over all directories in .\Plugins dir and add all Plugin* dirs to catalogs
foreach (var path in Directory.EnumerateDirectories(@".\Plugins", "Plugin*", SearchOption.TopDirectoryOnly))
{
    catalog.Catalogs.Add(new DirectoryCatalog(path));
}

_container = new CompositionContainer(catalog);
this._container.ComposeParts(this);

      

+11


source


There is also the following:   https://github.com/MefContrib/MefContrib/blob/master/src/MefContrib/Hosting/RecursiveDirectoryCatalog.cs



I found that I needed to wrap _aggregateCatalog.Catalogs.Add(catalog);

under Initialize(...)

with try / catch in ReflectionTypeLoadException

order to stop it metaing when it found dlls without plugins.

0


source







All Articles