我有一些作为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);