I think I've worked it out after reading this SO question.
I created a Metadata Attribute:
[MetadataAttribute] publicclass ExtensionMetadataAttribute : ExportAttribute, IExtensionMetadata { publicint ID { get; set; } public ExtensionMetadataAttribute(int id) : base(typeof (IExtension)) { ID = id; } }
Then modified ExtensionA.cs:
[ExtensionMetadata(1)] publicclass ExtensionA : IExtension { publicvoid DoWork() { System.Diagnostics.Debug.WriteLine("ExtensionA doing work.."); } }
And now ExtensionManager.cs looks like this:
publicclass ExtensionManager : IExtensionManager { privatereadonly IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> _extensions; public ExtensionManager(IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> extensions) { _extensions = extensions; } publicvoid DoWork(int extensionID) { foreach (var extension in _extensions) { if (extension.Metadata.ID == extensionID) { using (var foo = extension.CreateExport()) { foo.Value.DoWork(); } } } } }
This seems to do the trick, but I would still be interested in your feed back re best practices, performance issues etc.
Thanks!