How-To Prevent Module Duplicates with MEF?

梦想的初衷 提交于 2019-12-07 05:06:00

问题


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);
    }
    

回答1:


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.



来源:https://stackoverflow.com/questions/1708018/how-to-prevent-module-duplicates-with-mef

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!