MEF - How can i use an ExportProvider to select just one Export
i really dont get it work.
say i have some classes with the same interface which i want to export.
[Export(typeof(IService))]
[ExportMetadata("ExportType", typeof(Service1))]
public class Service1 : IService
{...}
[Export(typeof(IService))]
[ExportMetadata("ExportType", typeof(Service2))]
public class Service2 : IService
{...}
[Export(typeof(IService))]
[ExportMetadata("ExportType", typeof(Service3))]
public class Service3 : IService
{...}
now i have one class which import the IService and this class did not matter wich service it is.
public class Blup
{
[ImPortingConstructor]
public Blup(IService service)
{}
}
what i try to achieve now is to build an ExportProvider which select the right Export on Compose.
Something like this:
public TypeExportProvider<TTypeToExport>() : ExportProvider
{}
i really have no idea how the
protected override IEnumerable<Export> GetExportsCore(ImportDefiniti开发者_JAVA百科on definition, AtomicComposition atomicComposition)
should look like. maybe anyone know a blog to read about this. there a lot of custom ExportProvider samples out but not for this situation.
thx
Hi Kent if you look at the exports there are metadata called "ExportType", this should be the selector for the custom exportprovider. but after posting here a found a blog post from glenn block. so for my special purpose i have to do is this:
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
var defaults = new CatalogExportProvider(new TypeCatalog(typeof(Service2)));
var container = new CompositionContainer(catalog, defaults);
defaults.SourceProvider = container;
never the less it would be nice to see how can i write a custom exportprovider which select the right export from metadata information. my problem is how can a choose the right information from ImportDefnition and how could i set the new Export(...) in GetExportsCore().
This MSDN Magazine article explains how to retrieve and use export metadata. Essentially you use the GetExports() method on the CompositionContainer to obtain the types and the metadata, which then allows you to select the type based on the metadata values.
精彩评论