Lazy Loading DLLs with MEF

I am doing my first project with MEF and seriously cannot 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);

    }

I understand how to use MEF, except that I do not see how to initialize the DllList object. I want to use lazy loading, because in the final system we have many options, and only about 10% will be used at any time.

+5
source share
3 answers

First you try to import objects into a static property. This is not supported by MEF: MEF creates objects, not classes. If you want to initialize static properties, you must do this manually as follows:

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

: DirectoryCatalog AssemblyCatalog . AssemblyCatalog MEF , AssemblyCatalog.Parts, , . , , MEF , .

, , , - . MEF . , MEF codeplex, ComposablePartCatalogAssemblyCache.

, Lazy<T>, , . , .

+6

MEF ( ) ; MEF [], , MEF . , MEF , .

Lazy ( T) , , . , , , , , Value, .

MEF IoC ( MEF /), , IoC , -.

, - , : http://mef.codeplex.com/wikipage?title=Parts%20Lifetime

+1

This is a really old question, but for anyone looking for a solution, I recently implemented LazyAssemblyCatalogone that provides lazy loading from plugin modules, while still preserving the plugin metadata without having to download these assemblies. Its concept is very similar to CachedAssemblyCatalogthat @Wim mentioned in his answer. Hope this helps someone.

0
source

All Articles