我有一些dll文件插件。我的应用程序加载了dll,并且运行良好。但是当我尝试删除旧的插件并用新的插件替换它时,它却不允许我这样做。因为它已由应用程序加载。我发现通过使用appdomain我们可以做到这一点。但是我找不到使用mef的解决方案。
我需要一个可以在mef上运行的代码。下面是我的代码,用于加载插件。
//Creating an instance of aggregate catalog. It aggregates other catalogs
var aggregateCatalog = new AggregateCatalog();
//Build the directory path where the parts will be available
var directoryPath = "Path to plugins folder";
//Load parts from the available dlls in the specified path using the directory catalog
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");
//Add to the aggregate catalog
aggregateCatalog.Catalogs.Add(directoryCatalog);
//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);
// Composable parts are created here i.e. the Import and Export components assembles here
container.ComposeParts(this);
最佳答案
不幸的是,MEF不支持此功能。 MEF专为应用程序可扩展性而设计,而不是作为支持在运行时卸载和替换代码的通用插件系统。
进行此工作的唯一方法是在单独的AppDomain中使用MEF,然后整体卸载AppDomain。 CLR本身不支持卸载已加载的程序集,而是卸载在其中打开了该程序集的整个AppDomain。
关于c# - 卸载MEF中的DLL文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9494895/