MEF与ImportMany和ExportMetadata [英] MEF with ImportMany and ExportMetadata

查看:942
本文介绍了MEF与ImportMany和ExportMetadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始与托管扩展性框架玩耍。我有一个是出口类和import语句:

I've just started playing around with Managed Extensibility framework. I've got a class that's exported and a import statement:

[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获得出口类。不,我试图改变这个用懒列表,包括元数据,以便我可以筛选出我需要的类(相同的货物出口前)

This works just fine. The IEnumerable get the exported classes. No I try to change this to use the Lazy list and include the metadata so that I can filter out the class that i need (same export as before)

[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没有任何元素。我怀疑我在什么地方做一个明显的和愚蠢的错误。

After this the Ienumerable has no elements. I suspect that i've made an obvious and stupid mistake somewhere..

推荐答案

这可能是不匹配的,因为你的元数据接口没有按T对出口的元数据相匹配。为了配合你已经显示的样本出口,元数据接口应该是这样的:

It is probably not matching because your metadata interface doesn't match the metadata on the export. To match the sample export you've shown, your metadata interface should look like this:

public interface IMapMetaData
{
    int ID { get; }
}

这篇关于MEF与ImportMany和ExportMetadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆