vs-mef
vs-mef copied to clipboard
Offer an API to remove parts from ComposableCatalog
Although the workaround exists where a new ComposableCatalog
can be created with a subset of the parts from a prior catalog, it would be more convenient if a method were offered that returns a new catalog with the parts removed.
Potentially the removal criteria could be based on part type or export.
What does the workaround look like? 😛
Looks like reflection is needed?
If you wanted to include all parts from the assembly containing the Main
class, except that you wanted to substitute the IFoo
export with one from your tests, you could create the catalog like this:
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance);
DiscoveredParts discoveredParts = await discovery.CreatePartsAsync(typeof(Main).Assembly);
var parts = discoveredParts.Parts.Where(p => !p.ExportDefinitions.Any(ed => ed.Value.ContractName == typeof(IFoo).FullName));
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance)
.AddParts(parts)
.AddPart(discovery.CreatePart(typeof(MockFoo)));
Thanks. That will work for regular types.
Why not make the ContractNameServices
class public? That way you can use internal naming conventions whenever you run into hacky string comparisons as shown above.
Also, would you prefer checking ExportDefinitions
or ExportedTypes
?
ExportDefinitions
will allow you to remove the export whether you export from the type or a member, so it's more general and doesn't depend on the implementation detail within your application.
Regarding ContractNameServices
, we could make that public. Can you open a separate issue for that?