I'm trying to port some code from targeting the .NET Framework to .NET Core, and part of this involves switching from MEF1 to MEF2. There doesn't seem to be a great deal of documentation on using MEF in .NET Core, and I can't find any examples demonstrating how to filter out parts as was possible with MEF1.
In my original code (using MEF1), I wanted to load all parts exported from a set of assemblies except for MockCommunicationService
. I implemented this as follows:
// Filter out invalid exports.
Func<ComposablePartDefinition, bool> partFilter = it => !it.ToString().Contains(nameof(MockCommunicationService));
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var catalog = new DirectoryCatalog(assemblyPath).Filter(partFilter);
var container = new CompositionContainer(catalog);
What would the equivalent to this be in MEF2? I imagine that it probably involves using ConventionBuilder
, but I don't know if there's a way to define a convention along the lines of "permit everything except x".
Ideally, something like this would be great:
var conventions = new ConventionBuilder();
conventions.ForType<MockCommunicationService>().SuppressExports();
var configuration = new ContainerConfiguration()
.WithAssemblies(assemblies, conventions);