c# - MEF 从目录加载插件

标签 c# location mef .net-assembly

我与 MEF 合作,我正在寻找如何通过 MEF 找到插件的另一种方式更改插件位置的 url,我想更改这一行

Assembly.LoadFrom(@"C:\julia\project\project.Plugin.Nav\bin\Debug\NavPlugin.dll")));

我想删除这个 url,因为我需要在另一台机器上部署我的应用程序

这是我的功能:

public void AssembleCalculatorComponents()
{
   try
   {
       //var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
       //var container = new CompositionContainer(catalog);
       //container.ComposeParts(this);
       var catalog = new AggregateCatalog();

       catalog.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFrom(@"C:\yosra\project\project.Plugin.Nav\bin\Debug\NavPlugin.dll")));
       var container = new CompositionContainer(catalog);

       container.ComposeParts(this);
    }
    catch (Exception ex)
    {
       throw ex;
    }
 }

你能帮帮我吗?

谢谢

最佳答案

您可以使用 DirectoryCatalog类让 MEF 扫描特定目录以查找满足您的导入的程序集。

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("."));
var container = new CompositionContainer(catalog);

上面的代码会将 AppDomain 用于定位程序集的基本目录(通常是存放可执行文件的目录,除非随配置更改)添加到聚合目录。您可能还想添加当前正在执行的程序集,但这不是必需的。

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog("."));
var container = new CompositionContainer(catalog);

有关 DirectoryCatalog 的 MSDN 的更多信息:http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.hosting.directorycatalog.aspx

关于c# - MEF 从目录加载插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10986966/

相关文章:

c# - 带属性赋值的线程安全构造

android - java : Convert Location into String and back

c# - 泛型类型 MyType<,> 实际上是一个类型吗?

c# - 如何在C#中加密/解密url

C#角色服装系统

android - Flurry Analytics 可以在 Android 上按城市报告用户的位置吗?

javascript - Android webview位置检测javascript

c# - Visual Studio 2010 MEF 与 MPF?

c# - 实现接口(interface)和匹配属性的 MEF 加载类型

c# - 反序列化 XML 为集合属性返回 null