Właśnie zacząłem grać ze strukturą Managed Extensibility. Mam wyeksportowaną klasę i instrukcję importu:MEF z ImportMany i ExportMetadata
[Export(typeof(IMapViewModel))]
[ExportMetadata("ID",1)]
public class MapViewModel : ViewModelBase, IMapViewModel
{
}
[ImportMany(typeof(IMapViewModel))]
private IEnumerable<IMapViewModel> maps;
private void InitMapView()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ZoneDetailsViewModel).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
foreach (IMapViewModel item in maps)
{
MapView = (MapViewModel)item;
}
}
Działa to dobrze. IEnumerable pobiera wyeksportowane klasy. Nie staram się to zmienić, aby skorzystać z listy leniwy i zawierają metadane tak, że można odfiltrować klasę, że muszę sama eksportowej (jak poprzednio)
[ImportMany(typeof(IMapViewModel))]
private IEnumerable<Lazy<IMapViewModel,IMapMetaData>> maps;
private void InitMapView()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ZoneDetailsViewModel).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
foreach (Lazy<IMapViewModel,IMapMetaData> item in maps)
{
MapView = (MapViewModel)item.Value;
}
}
Po tym IEnumerable ma żadnych elementów. Podejrzewam, że gdzieś popełniłem oczywistą i głupią pomyłkę ..
Jak wygląda twój interfejs metadanych? –
Nie wiedziałem, że można wykonać ImportMany, w tym metadane. miły! – juFo