在应用程序运行时如何发现新的MEF零件? [英] How to discover new MEF parts while the application is running?

查看:72
本文介绍了在应用程序运行时如何发现新的MEF零件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MEF在我的应用中加载插件.一切正常,但是当我将新部件放到我的应用程序文件夹中时,我希望它们能被发现.这可能吗? DirectoryCatalog有一个Changed事件,但是我不确定它如何工作.

I'm using MEF to load plugins in my app. Everything works, but I want new parts to be discovered when they are dropped into my app folder. Is this possible? DirectoryCatalog has a Changed event but I'm not sure how it works.

这是我现在的代码:

public sealed class RevealerFactory
{
    private static readonly Lazy<RevealerFactory> lazy = 
            new Lazy<RevealerFactory>(() => new RevealerFactory());

    public static RevealerFactory Instance { get { return lazy.Value; } }

    private FileSystemWatcher watcher;

    private RevealerFactory()
    {
        Initialize();
    }

    [ImportMany(RequiredCreationPolicy = CreationPolicy.Shared)]
    private IEnumerable<Lazy<IRevealer, IRevealerCapabilities>> Revealers { 
        get;
        set; 
    }

    public IRevealer GetRevealer(Uri uri)
    {
        return (from revealer in Revealers
                where uri.Host.Equals(revealer.Metadata.Host, 
                                      StringComparison.OrdinalIgnoreCase) 
                   && revealer.Value.IsRevelable(uri)
                select revealer.Value).FirstOrDefault();
    }

    private void Initialize()
    {
        var catalog = new DirectoryCatalog(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
                                      + "/SDownloader/Revealers");
        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }
}

推荐答案

您可以使用 FileSystemWatcher 来检测正在您的插件文件夹中删除的新DLL.然后,您可以通过调用 DirectoryCatalog.Refresh AggregateCatalog.Catalogs.Add 用新零件更新MEF组成.

You can use the FileSystemWatcher to detect new DLLs being dropped in your plugin folder. Then you can handle such events by calling DirectoryCatalog.Refresh or AggregateCatalog.Catalogs.Add to update the MEF composition with the new parts.

一些需要注意的事情:

  1. 您需要将MEF导入标记为旨在处理重组,如

  1. You need to mark your MEF imports as being designed to deal with recomposition, as explained in the MEF programming guide section on Recomposition. Otherwise MEF will raise an error when you try to update them.

FileSystemWatcher在系统线程池线程上引发事件(除非您使用

FileSystemWatcher raises events on system thread pool threads (unless you make use of the SynchronizingObject property). Be aware that if you call DirectoryCatalog.Refresh from another thread, you must construct the CompositionContainer with the isThreadSafeFlag enabled. You'll also have to think about the thread safety of your property setters that will be called when the composition is updated.

您还可以通过将目录从AggregateCatalog.Catalogs集合中删除来删除目录.但是无法卸载关联的程序集(除非卸载整个Appdomain).这也意味着您仍无法在应用程序运行时删除或覆盖程序集.

You can also remove catalogs by taking them out of an AggregateCatalog.Catalogs collection. But there is no way to unload the associated assemblies (except by unloading the entire Appdomain). That also means you still can't delete or overwrite an assembly while the application is running.

这篇关于在应用程序运行时如何发现新的MEF零件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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