tags:

views:

34

answers:

2

Hi,

I have a solution with the following structure:

Solution

Main Exe

Utilities

When I use MEF within the Utilities project I find that neither of the following MEF catalogues pick up types held within the Main Exe

catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

catalog.Catalogs.Add(new DirectoryCatalog(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory)));

i assume the first fails as its being called within the utilities project, and that the second fails since the types in the main project are stored in an EXE and not a DLL...

what is the correct way to get a Mef catalogue which finds all types in all the projects of a solution?

+1  A: 

Just Spotted Assembly.GetEntryAssembly() so the following will work

AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));  

although it does not load the other types in the solution - one option is to use a Directory catalogue though i wonder if there is some way to avoid using this as it raises the possibility of introducing 3rd party types where i dont immediatly want them?

GreyCloud
A: 

In my projects I simply put a command in the post-build events, which copies the output into the main apps bin directory:

XCOPY "$(ProjectDir)$(OutDir)*" "$(SolutionDir)MainApp\bin\*" /y

Then I just load from the root directory path:

AggregateCatalog Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new DirectoryCatalog(root_directory_path, "My.Assemlies.*"));
CompositionContainer Container = new CompositionContainer(Catalog);
Container.ComposeParts(this);
Tim