[英]Unloading a dll file in mef
我有一些插件作為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);
我發現通過使用appdomain,我們可以做到這一點。 但我無法找到使用mef的解決方案。
不幸的是,MEF不支持這一點。 MEF專為應用程序可擴展性而設計,而不是作為支持在運行時卸載和替換代碼的通用插件系統。
實現這項工作的唯一方法是在單獨的AppDomain中使用MEF,並將AppDomain作為一個整體卸載。 除了卸載打開程序集的整個AppDomain之外,CLR本身不支持卸載已加載的程序集。
如果您嘗試在目錄上插入一個程序集對象,如下所示:
Assembly assembly = Assembly.Load(System.IO.File.ReadAllBytes(Path.Combine(directoryPath, ItemPlugin)));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(assembly));
您可以稍后刪除/更改文件...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.