Import part with specific meta data using MEF Preview 5

Posted by Riri on Stack Overflow See other posts from Stack Overflow or by Riri
Published on 2009-05-19T17:35:07Z Indexed on 2010/04/28 20:37 UTC
Read the original article Hit count: 212

Filed under:
|

I have a export defined as as follows in MEF preview 5

[ExportMetadata("Application", "CheckFolderApplication")]
[Export(typeof(ExtendedArtifactBase))]
public class CheckFolderArtifact2 : ExtendedArtifactBase
{ ...

Then I only want those imports with the "Application" "CheckFolderApplication" metadata. To currenly do that I read all the imports and then filter the result.

[Import(typeof(ExtendedApplicationBase))]
private ExportCollection<IApplication> _applications { get; set; }

public IApplication GetApplication(string applicationName)
{
    return _applications.Single(a => a.GetExportedObject().Name == applicationName).GetExportedObject();
 }

This feels really inefficient. What if I have thousands of plug-ins - do I have to read them all via MEF to just get one with the right metadata? If so how do you cache the result?

© Stack Overflow or respective owner

Related posts about MEF

Related posts about c#