I have a AggregateCatalog that contains an AssemblyCatalog and a DirectoryCatalog.
I want them to work like this:
- If both catalogs can find an export, choose the one from the DirectoryCatalog.
- If neither of them can find an export, then just leave the import to be null.
- If only one of them can find an export, then just use that export to fill the import.
How can I achieve something like this?
You can achieve point 1. and 3. by putting the catalogs in different export providers, and then passing the export providers to the
CompositionContainerconstructor in order of priority like this:Now you can use the
containeras usual, and it will look for parts in the directory catalog first, assembly catalog second. You won’t get cardinality exceptions when it is present in both.To achieve point 2., you have to mark individual imports to allow the default value (e.g.
null) with[Import(typeof(SomeType),AllowDefault=true].