Import parts with specific metadata using MEF Preview 5

I have the export specified in MEF preview 5

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

Then I just want to import the data with the application metadata "CheckFolderApplication". To do this, I read the entire import, and then filtered out 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();
 }

It seems very inefficient. What if I have thousands of plugins. Do I have to read them through MEF to get one of them with the correct metadata? If so, how do you cache the result?

+3
source share
2 answers

Yes, in this case you will have to filter yourself.

, . ( AllowRecomposition import true), IPartImportsSatisfiedNotification , OnImportsSatisfied , .

+3

, - . Lazy , , , .

[ImportMany(typeof(MyInterface))]
List<Lazy<MyInterface,MyMetadataType>> MyGuys { get; set; }

( - , MyMetadataType , TMetadata = IDictionary)

, MEF - " ", ...

0

Source: https://habr.com/ru/post/1708678/


All Articles