Caution: this is not a common way to use MEF. But since you asked ... you can use GetExports overload , which accepts ImportDefinition .
To find out which contract name you should use for this type, you can call AttributedModelServices.GetContractName(typeof(IPlugin))
. Usually this is just the full type name.
The exact type of metadata is not important - all that matters is the metadata properties declared on it. You can describe them as in the requiredMetadata
dictionary below.
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); string contractName = "SomeNamespace.IPlugin"; var requiredMetadata = new Dictionary<string, Type>(); requiredMetadata["Meta1"] = typeof(string); requiredMetadata["Meta2"] = typeof(int); var importDefinition = new ContractBasedImportDefinition( contractName, null, requiredMetadata, ImportCardinality.ZeroOrMore, false, true, CreationPolicy.Any); var exports = container.GetExports(importDefinition); Console.WriteLine(exports.Count()); Console.ReadKey();
source share