0
votes

I've used MEF before, but am having problems now with the newer implementation. I'm exporting objects inheriting from a base class, that has the [InheritedExport] attribute, and am trying to get them imported.

Before, I would just have an array property with [ImportMany(typeof(mytype))], and use CompositionInitializer.SatisfyImports(this) to get the imports to work, but now I have two problems:

1) The exports are in a different, referenced assembly; 2) CompositionInitializer doesn't seem to exist in the newer MEF in the 4.5 framework.

I'm trying to create an AggregateCatalog to solve problem #1, but have no idea where to go from here.

In the end, I'm trying to import a collection that can be used by the entire WPF app, if anyone can give me any assistance in suggesting an overall solution.

1
MEF2 is backward compatible with MEF as far as I know. Are you moving from Silverlight to WPF?Panos Rontogiannis
Yes. That would explain #2, abot the CompositionInitializer. Didn't realize that was a Silverlight only class.Random

1 Answers

1
votes

1) The exports are in a different, referenced assembly;

As you say, you will need the AggregateCatalog to aggregate a number of catalogs. Usually you will need one catalog for each assembly that contains a part (export/import). This means that you will have to use the AssemblyCatalog class for each one of theses loaded assemblies. You can access the loaded assemblies through one of the types that they contain. Here's a small example that adds a couple of AssemblyCatalogs to an AggregateCatalog.

AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(SomeClassInSomeAssembly).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(SomeClassInAnotherAssembly).Assembly));

For more information on catalogs you can read this small article.

2) CompositionInitializer doesn't seem to exist in the newer MEF in the 4.5 framework.

This is only available on Silverlight. On WPF you will have to create a CompositionContainer and use its SatisfyImports method.

CompositionContainer container = new CompositionContainer(catalog);
container.SatisfyImports(someObjectWithImports);