Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-To Prevent Module Duplicates with MEF?

Tags:

c#

mef

How can i prevent from MEF to load duplicates Modules in the case of the presence of 2 copies of the same Assembly (maybe 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);
    }
    
like image 841
Yoann. B Avatar asked Nov 05 '22 18:11

Yoann. B


1 Answers

Instead of using a DirectoryCatalog, use an AggregateCatalog. You will have to write code that looks at all the Assemblies in the modules directory, figures out if the current one is a duplicate of one it has already processed, and if not, creates an AssemblyCatalog for that Assembly and adds it to the AggregateCatalog.

I'm not sure exactly what logic you would be able to use to detect that two DLLs with different names are the "same" assembly, though.

like image 142
Daniel Plaisted Avatar answered Nov 14 '22 13:11

Daniel Plaisted