Managed Extensibilityフレームワークを使い始めたばかりです。私はエクスポートされたクラスとインポートステートメントを持っています:ImportManyとExportMetadataを使用したMEF
[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;
}
}
これはうまくいきます。 IEnumerableは、エクスポートされたクラスを取得します。ノー私はIEnumerableをに要素がない。この後
[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;
}
}
私は(以前と同じ輸出を)必要とするクラスを除外することができるように怠惰なリストを使用してメタデータを含めるために、これを変更してみてください。
メタデータインターフェイスはどのように見えますか? –
メタデータを含むImportManyを実行できるかどうかはわかりませんでした。いいよ! – juFo