Search code examples
c#mef

Detecting a MEF assembly at runtime


I have a directory with many .dlls some of which are MEF plugins which I am loading using a DirectoryCatalog - for example:

var catalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

This loads into the catalog all assemblies at hosts executing directory. However, I wish to only build a catalog of MEF assemblies (i.e. composable parts).

Is there a way to detect MEF assemblies?


Solution

  • Those assemblies without MEF parts do not have any effect on DirectoryCatalog.Parts, so MEF already does the detection for you.

    If you feel that the performance impact of scanning all assemblies for MEF parts is too high, then you can use a search pattern to filter on the DLL name like this:

    var catalog = new DirectoryCatalog(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
        "*.plugins.dll");