Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MEF recursive plugin search

Let's say that I have a few applications in a folder (each application has subfolders where plugins can be located):

  • Clients
    • Application A
      • ...
    • Application B
      • ...
    • Application C
      • ...
    • ...

Some files in these applications have an Export-attribute applied, others don't. Now, I want to be able to load these plugins in some of these applications. Is there a proper way to let MEF search recursively in every subfolder of a specified folder?

like image 689
Bram W. Avatar asked Nov 15 '11 15:11

Bram W.


3 Answers

No, you will need to recurse through the directories yourself creating a DirectoryCatalog for each. Then, combine all of the DirectoryCatalogs with an AggregateCatalog to create the container.

like image 152
Nicholas Blumhardt Avatar answered Nov 09 '22 09:11

Nicholas Blumhardt


Another way is to get all the DLL files under a specified directory (recursively) and load them one by one using the assembly catalog.`

var catalog = new AggregateCatalog();

        var files = Directory.GetFiles("Parent Directory", "*.dll", SearchOption.AllDirectories);

        foreach (var dllFile in files)
        {

            try
            {
                var assembly = Assembly.LoadFile(dllFile);
                var assemblyCatalog = new AssemblyCatalog(assembly);
                catalog.Catalogs.Add(assemblyCatalog);
            }
            catch (Exception e)
            {
               // this happens if the given dll file is not  a .NET framework file or corrupted.

            }
        }
like image 36
Anas Ghanem Avatar answered Nov 09 '22 08:11

Anas Ghanem


There's a MEFContrib project available that has a RecursiveDirectoryCatalog for just this purpose...

https://www.nuget.org/packages/MefContrib/

like image 34
Random Avatar answered Nov 09 '22 10:11

Random