延迟加载DLL的与MEF [英] Lazy Loading DLL's with MEF

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

问题描述

我做我的MEF的第一个项目,并很认真地无法了解如何使用延迟加载。我的code为 -

I'm doing my first project with MEF and am seriously unable to understand how to use lazy loading. My code is -

public static class MefLoader
{
     private static CompositionContainer Container;

    [ImportMany(typeof(IControlModule), AllowRecomposition = true)]
    private static IEnumerable<Lazy<IControlModule, IImportComponentCapabilites>> 
               DllList { get; set; }

    static MefLoader()
    {
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog("."));
        Container = new CompositionContainer(catalog);

    }

据我所知大部分如何使用MEF,但我不明白如何初始化DllList对象。我想用延迟加载,因为在最后的系统,我们有很多的选择,只有约10%会在任何一个时间使用。

I understand most of how to use MEF, except that I don't see how to initialize the DllList object. I want to use lazy loading because in final system, we have a great many options and only about 10% are going to be used at any one time.

推荐答案

首先,您要导入的对象成静态属性。这是不支持通过MEF:MEF组成的对象的,不会的的。如果你想要初始化静态属性,你必须手动操作这样的:

First, you are trying to import objects into a static property. This is not supported by MEF: MEF composes objects, not classes. If you want to initialize static properties, you have to do it manually like this:

DllList = container.GetExports<IControlModule, IImportComponentCapabilites>();

现在关于延迟加载: DirectoryCatalog 创建一个 AssemblyCatalog 为目录中的每个组件。在MEF的 AssemblyCatalog 的实施将枚举所有类型的组件,只要 AssemblyCatalog.Parts 被调用,它会发生当你拉从容器的出口。这意味着,即使前MEF已经确定它包含实际需要的部分即集加载

Now about lazy loading: DirectoryCatalog creates a AssemblyCatalog for each assembly in the directory. The AssemblyCatalog implementation in MEF will enumerate all types in the assembly as soon as AssemblyCatalog.Parts is called, which will happen when you pull an export from the container. This means that the assembly is loaded even before MEF has determined that it contains a part that it actually needs.

为了真正拥有组件的延迟加载,部件可在这些组件哪些信息需要被缓存的地方。 MEF目前没有这样的内置缓存机制开箱。然而,在样品中 ComposablePartCatalogAssemblyCache 执行包含在 MEF源$ C ​​$下以codePLEX

In order to truly have lazy loading of assemblies, the information about which parts are available in those assemblies would need to be cached somewhere. MEF currently does not have such a built-in cache mechanism out of the box. However, there is a ComposablePartCatalogAssemblyCache implementation in the samples included with the MEF source code at codeplex.

唯一的延迟&LT; T&GT; 确实是推迟时刻的的构造的部分被称为。这已经可以加快东西,但它不会推迟组件的装载。

The only thing that Lazy<T> does is postpone the moment that the constructor of the part is called. This can already speed up things but it won't postpone the loading of assemblies.

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

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