I have a AggregateCatalog that contains an AssemblyCatalog and a DirectoryCatalog.
I want them to work like this:
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 CompositionContainer
constructor in order of priority like this:
var dirCatalog = new DirectoryCatalog(...);
var provider1 = new CatalogExportProvider(dirCatalog);
var assemblyCatalog = new AssemblyCatalog(...);
var provider2 = new CatalogExportProvider(assemblyCatalog);
var container = new CompositionContainer(provider1, provider2);
// link the export providers back to the container, so that they can
// resolve parts from other export providers
provider1.SourceProvider = container;
provider2.SourceProvider = container;
Now you can use the container
as 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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With