MEF - 部分重组

时间:2014-10-25 19:35:39

标签: c# .net mef

我想知道是否有可能改变MEF行为以进行部分重组。想象一下,我们已经将3个插件加载到Plugins集合中。

当新的dll放在正确的目录中时,MEF默认会重新组合所有插件。 我想归档的只是添加新的插件到我的集合并保留现有的实例。这有可能吗?

请在下面找到源代码:

internal class ServerPluginManager : MarshalByRefObject , IServerPluginManager
{
    private readonly string _pluginDirectory;

    public DirectoryCatalog PluginCatalog;
    public CompositionContainer CompositionContainer;
    public FileSystemWatcher PluginWatcher;

    [ImportMany(AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.Shared)]
    public IEnumerable<Lazy<IServerPlugin>> Plugins { get; set; }

    public ServerPluginManager(string pluginDirectory)
    {
        _pluginDirectory = pluginDirectory;
    }

    public bool Start()
    {
        try
        {
            PluginCatalog = new DirectoryCatalog(_pluginDirectory);
            CompositionContainer = new CompositionContainer(PluginCatalog, new ConfigurationExportProvider(new FileConfigurationSource()));
            CompositionContainer.ComposeParts(this);

            string domainName = AppDomain.CurrentDomain.FriendlyName;

        }
        catch (ReflectionTypeLoadException ex)
        {

            return false;
        }

        Plugins.OnEach(plugin => plugin.Run());

        PluginWatcher = new FileSystemWatcher(_pluginDirectory, "*.dll")
        {
            EnableRaisingEvents = true,
            IncludeSubdirectories = false,
        };
        PluginWatcher.Changed += PluginWatcherOnChanged;
        PluginWatcher.Created += PluginWatcherOnCreated;
        PluginWatcher.Deleted += PluginWatcherOnDeleted;
        PluginWatcher.Renamed += PluginWatcherOnRenamed;
        PluginWatcher.Error += PluginWatcherOnError;

        return true;
    }

    public void Dispose()
    {
        PluginWatcher.Changed -= PluginWatcherOnChanged;
        PluginWatcher.Created -= PluginWatcherOnCreated;
        PluginWatcher.Deleted -= PluginWatcherOnDeleted;
        PluginWatcher.Renamed -= PluginWatcherOnRenamed;
        PluginWatcher.Error -= PluginWatcherOnError;
        PluginWatcher = null;

        Plugins.OnEach(plugin => plugin.Dispose());
    }

    private void PluginWatcherOnChanged(object sender, FileSystemEventArgs fileSystemEventArgs)
    {
        PluginCatalog.Refresh();
        Plugins.Where(x => x.Value.State == PluginState.Stopped).OnEach(plugin => plugin.Run());
    }

    private void PluginWatcherOnCreated(object sender, FileSystemEventArgs e)
    {
        PluginCatalog.Refresh();
        Plugins.OnEach(plugin => plugin.Run());
    }

    private void PluginWatcherOnDeleted(object sender, FileSystemEventArgs e)
    {
        PluginCatalog.Refresh();
        Plugins.OnEach(plugin => plugin.Run());
    }

    private void PluginWatcherOnRenamed(object sender, FileSystemEventArgs e)
    {
        PluginCatalog.Refresh();
        Plugins.OnEach(plugin => plugin.Run());
    }

    private void PluginWatcherOnError(object sender, ErrorEventArgs e)
    {
        PluginCatalog.Refresh();
        Plugins.OnEach(plugin => plugin.Run());
    }
}

0 个答案:

没有答案