tags:

views:

424

answers:

2

I have a MEF (Microsoft Extension Framework) application which loads some assemblies from a folder. I need to enumerate the assemblies that produced any exports for my application.

One way to do it is to enumerate imports calling GetExportedObject().GetType().Assembly. But it would be cleaner to do this without instantiation of imports. Is there a way to get loaded assemblies from a catalog or anything else?

I need assemblies to get their attributes like copyright, version, name and such. My folder can contain both assemblies with exports and without ones, but I need only assemblies that satisfied any imports in the app.

+1  A: 

The AssemblyCatalog has an Assembly property. The AggregateCatalog does not have a way to get this information directly- there's no guarantee that the inner catalogs even load their parts from an assembly. The DirectoryCatalog doesn't have this capability although it might be possible to add it if there was a good reason.

Why do you want to get the list of assemblies? You may be better off not using a directory catalag, instead just scan and load the assemblies in a directory yourself, and create an AssemblyCatalog for each one and add it to an AggregateCatalog.

EDIT: MEF doesn't have a way of getting a list of all the exports that were "used" in composition. You could probably write your own catalog which would return part definitions that were shells around the default part definitions and kept track of which parts had GetExportedObject called on them. You can use the APIs in ReflectionModelServices to figure out which type corresponds to a given part definition from the default catalogs. Note that writing such a catalog would probably not be a simple undertaking.

Daniel Plaisted
I've updated the question on why I need the assemblies. Actually, even having DirectoryCatalog return the assemblies wouldn't solve my goal because the list might contain MEF dll's which have nothing to do with _my_ imports. If I could get the assembly from Export without calling the GetExportedObject, or have the satisfied imports assemblies some other way...
Sergey Aldoukhov
I'll add that I need this same sort of functionality. I'm integrating with another system that uses the BuildManager and I want to add assemblies that I'm using with MEF to the BuildManager.
Brian Vallelunga
A: 

Here is my current solution that works nicely:

  1. Do not use DirectoryCatalog, load assemblies directly and create an AssemblyCatalog from them.
  2. Use AssemblyCatalog.Parts to find out which assemblies have exports, let the user authorize them.
  3. Add only authorized AssemblyCatalog's to the AggregateCatalog, which is ised in the composition.
Sergey Aldoukhov