How to prevent duplication of modules using MEF?

How can I prevent MEF from loading duplicate Modules if there are 2 copies of the same assembly (possibly by mistake)

  • Assembly1.dll

  • Assembly2.dll (copy of Assembly1)

    [ImportMany]
    public IList<IModule> Modules { get; private set; }
    
    public void BuildUp()
    {
        Modules = new List<IModule>();
    
        var catalog = new DirectoryCatalog(@".\Modules");
        var container = new CompositionContainer(catalog);
    
        container.ComposeParts(this);
    }
    
+5
source share
1 answer

Instead of using DirectoryCatalog, use AggregateCatalog. You will need to write code that will look through all the assemblies in the module directory, and find out if the current instance of one of them that it has already processed, and if not, creates an AssemblyCatalog for this assembly and adds it to AggregateCatalog.

, , , DLL " " .

+2

All Articles