如何在运行时移除MEF插件吗? [英] How to remove MEF plugins at runtime?

查看:539
本文介绍了如何在运行时移除MEF插件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以使用插件定制基于MEF的应用程序。此应用程序有一些进口零件,我想在运行时删除其中的一些(要能删除包含它们的.DLL)当用户决定要摆脱插件。



CompositionBatch会做什么,我需要,但它需要 ComposablePart 为的 RemovePart() 方法,我只有实现ISomething接口平原对象,在 AggregateCatalog ComposablePartDefinition 实例。所以,我的问题是:




  • 我如何才能找到一个代表进口
    对象ComposablePart
    实例,我?想摆脱

  • 或者
    或者:我怎么属于
    到一定.DLL ComposablePart对象列表
    <? / LI>


我想如下使用的东西:

 变种份= Container.Catalog.Parts 
。凡(p值=> iDontNeed(p))
。选择(p值=> howDoIConvertComposablePartDefinition2ComposablePart(p));
变种一批=新CompositionBatch();
parts.ToList()的ForEach(部分=> batch.RemovePart(部分));

感谢您


解决方案

我如何获得属于某个.DLL ComposablePart对象的列表?




要从一个特定的部件上卸下部分,你可以只删除 AssemblyCatalog 致电 AggregateCatalog.Catalogs.Remove 。你必须设计自己的部件,以便的重构虽然。



不过,这不会帮你删除插件组装。大会将仍然被加载并不能更改或当它删除加载.NET程序集。到的唯一途径的卸载的不停车过程中的装配是通过卸载的AppDomain 在其中加载。但是,如果你介绍一个单独的的AppDomain 的插件,那么你基本上都通过远程处理这些插件,等等沟通。


$ B $ :b

这可能更简单,更安全和有效的,只是停止应用程序,请删除插件组装,然后重新启动



修改:实际上,有的方式来删除插件dll文件,而不停止进程或卸载整个应用程序域:您可以启用的shadow复制为应用程序域指示.NET加载在大会上进行复印。副本将保持加载,但至少你可以删除或替换原来的文件。


I have a MEF-based application that can be customized with plugins. This application has several imported parts, and I want to remove some of them at runtime (to be able to delete the .dll that contains them) when a user decides to get rid of that plugin.

CompositionBatch would do what I need, but it needs ComposablePart instances as input parameters for RemovePart() method, and I only have plain objects that implement ISomething interface, or ComposablePartDefinition instances in the AggregateCatalog. So the my question is:

  • How can I find the ComposablePart instance that represents the imported object that I want to get rid of?
  • Or alternatively: how do I get the list of ComposablePart objects that belong to a certain .dll?

I would use something like follows:

var parts = Container.Catalog.Parts
           .Where(p => iDontNeed(p))
           .Select(p => howDoIConvertComposablePartDefinition2ComposablePart(p));
var batch = new CompositionBatch();
parts.ToList().ForEach(part => batch.RemovePart(part));

Thank you

解决方案

how do I get the list of ComposablePart objects that belong to a certain .dll?

To remove the parts from a particular assembly, you could just remove that AssemblyCatalog by calling AggregateCatalog.Catalogs.Remove. You'll have to design your parts to allow for Recomposition though.

However, this will not help you to delete the plugin assembly. The assembly will still be loaded and you cannot change or delete a .NET assembly while it is loaded. The only way to unload an assembly without stopping the process is by unloading the AppDomain in which it loaded. But if you introduce a separate AppDomain for the plugins then you'll basically have to communicate with those plugins via remoting, etcetera.

It's probably much simpler, safer and effective to just stop the application, delete the plugin assembly, and restart.

edit: actually, there is a way to delete the plugin dll file without stopping the process or unloading the entire appdomain: you can enable shadow copying for the app-domain to instruct .NET to make a copy before loading the assembly. The copy will remain loaded, but at least you can delete or replace the original file.

这篇关于如何在运行时移除MEF插件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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