如何防止模块与MEF重复? [英] How-To Prevent Module Duplicates with MEF?

查看:78
本文介绍了如何防止模块与MEF重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果存在相同程序集的2个副本(可能是错误的),我如何防止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

  • Assembly1.dll

Assembly2.dll(Assembly1的副本)

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

推荐答案

使用AggregateCatalog代替使用DirectoryCatalog.您将必须编写代码,以查看modules目录中的所有程序集,确定当前程序集是否与已处理的程序集重复,如果不是,则为该程序集创建AssemblyCatalog并将其添加到AggregateCatalog.

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.

我不确定您将使用哪种逻辑来检测两个名称不同的DLL是相同"程序集.

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.

这篇关于如何防止模块与MEF重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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