vs-mef icon indicating copy to clipboard operation
vs-mef copied to clipboard

Offer an API to remove parts from ComposableCatalog

Open AArnott opened this issue 6 years ago • 4 comments

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.

AArnott avatar Mar 28 '18 19:03 AArnott

What does the workaround look like? 😛

Looks like reflection is needed?

znakeeye avatar Aug 14 '19 13:08 znakeeye

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)));

AArnott avatar Aug 14 '19 14:08 AArnott

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?

znakeeye avatar Aug 14 '19 14:08 znakeeye

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?

AArnott avatar Aug 14 '19 14:08 AArnott