如何指定使用 System.Composition (MEF2) 加载哪些 DLL?
How to specify which DLLs to load with System.Composition (MEF2)?
我正在开发一个新的插件加载器应用程序框架并阅读 MEF 的所有可用变体,我认为最具前瞻性和灵活性的是 NuGet 提供的 MEF2 实现 System.Composition
。
不幸的是,大多数(如果不是全部)在线教程都是为 MEF1 编写的,即 System.ComponentModel.Composition
并使用从例如创建的 AssemblyCatalog
在目录中明确查找 DLL 并将其传递给 MEF CompositionContainer
.
MEF2 没有这个对象,因此我仍然不清楚 MEF2 在哪里找到它的组件。它们不能在运行时定位吗(这意味着需要添加每个插件作为对加载插件的应用程序的引用)?那会让我觉得很奇怪。
谁能阐明如何在 System.Composition
中指定从何处加载哪些程序集(以及我到底应该如何设置)?
我知道 MEF2 在设计时考虑了 PCL(我正在编写桌面应用程序),但如果为这种未来的可移植性支付的安装费用很小,我完全赞成。如果没有,我会屈服于使用 System.ComponentModel.Composition
我能弄清楚。
目录替换为 ContainerConfiguration:
var conf = new System.Composition.Hosting.ContainerConfiguration();
conf.WithAssembly(Assembly.GetExecutingAssembly());
var container = conf.CreateContainer();
var hwWriter = new HelloWorldWriter();
container.SatisfyImports(hwWriter);
Console.WriteLine(hwWriter.Write);
Console.ReadLine();
在 ContainerConfiguration
中,您会发现更多 With...
方法,它们替换了 MEF1 中的所有不同目录。
我正在开发一个新的插件加载器应用程序框架并阅读 MEF 的所有可用变体,我认为最具前瞻性和灵活性的是 NuGet 提供的 MEF2 实现 System.Composition
。
不幸的是,大多数(如果不是全部)在线教程都是为 MEF1 编写的,即 System.ComponentModel.Composition
并使用从例如创建的 AssemblyCatalog
在目录中明确查找 DLL 并将其传递给 MEF CompositionContainer
.
MEF2 没有这个对象,因此我仍然不清楚 MEF2 在哪里找到它的组件。它们不能在运行时定位吗(这意味着需要添加每个插件作为对加载插件的应用程序的引用)?那会让我觉得很奇怪。
谁能阐明如何在 System.Composition
中指定从何处加载哪些程序集(以及我到底应该如何设置)?
我知道 MEF2 在设计时考虑了 PCL(我正在编写桌面应用程序),但如果为这种未来的可移植性支付的安装费用很小,我完全赞成。如果没有,我会屈服于使用 System.ComponentModel.Composition
我能弄清楚。
目录替换为 ContainerConfiguration:
var conf = new System.Composition.Hosting.ContainerConfiguration();
conf.WithAssembly(Assembly.GetExecutingAssembly());
var container = conf.CreateContainer();
var hwWriter = new HelloWorldWriter();
container.SatisfyImports(hwWriter);
Console.WriteLine(hwWriter.Write);
Console.ReadLine();
在 ContainerConfiguration
中,您会发现更多 With...
方法,它们替换了 MEF1 中的所有不同目录。