views:

45

answers:

3

hi, 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(ImportDefinition 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

A: 

What is your criteria for selecting "the right export"? Can you just add some metadata and use that to select it? You need to provide more information about what it is you're trying to achieve.

HTH,
Kent

Kent Boogaart
A: 

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().

blindmeis
marked as answer cause it solves my problem.
blindmeis
A: 

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.

using metadata for imports while using lazy<> is not the problem. my question is how can i treat the mef composition(ExportProvider) to just select the ONE export while there many exports to satisfy my ExatlyOneImport.
blindmeis
That depends on your criteria for selecting one export. GetExports().First() or use a LINQ query on the result of GetExports().