我刚刚开始使用托管可扩展性框架。我有一个导出的类和一个导入语句:
[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 获取导出的类。不,我尝试更改它以使用惰性列表并包含元数据,以便我可以过滤掉我需要的类(与以前相同的导出)
[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;
}
}
此后,Ienumerable 就没有元素了。我怀疑我在某个地方犯了一个明显而愚蠢的错误。
它可能不匹配,因为您的元数据接口与导出的元数据不匹配。为了匹配您显示的示例导出,您的元数据界面应如下所示:
public interface IMapMetaData
{
int ID { get; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)