C# .NET 4.0 表单插件管理器

发布于 2024-10-15 01:29:14 字数 612 浏览 7 评论 0 原文

我对 MEF、MAF、Unity 有点迷失......

这个问题是关于 Winform 应用程序的架构。

我有一个主项目,其中包含一个主窗体和一些其他窗体;

因为我想包含模块化,所以我正在考虑使用插件系统。

我想做的是在主应用程序打开时打开每个插件 Dll,以引用每个插件的按钮、工具栏...

然后我想处置它们,直到它们被称为。

但我不希望所有插件都保存在内存中..只是为了获得一个好的架构模型。

首先介绍一下 .NET: dotNet 是否仅在内存中保留对 dll 插件的引用,还是所有插件代码

我正在考虑将 MEF 与 LAZY 导入集合一起使用,但我需要先实例化它们才能获取按钮信息。所以第二个问题

如果我将导入集合设置为 null 并再次启动 compose() 函数,插件将加载还是等待调用加载(惰性)?

I'm a bit lost with MEF, MAF, Unity ...

This question is about architecture of Winform application.

I've got a main project which contains a Main form and some other forms;

Because i want to include modularity, i'm thinking of using a Plugin System.

What I would like to do is opening each Plugin Dll when the Main Application is opened to reference each with button, toolbar ...

Then i would like to dispose them until they are called.

But i don't want all the plugins to be kept in memory.. just to got a good architecture model.

So first about .NET :
Does dotNet keep only a reference to the dll plugins in memory or all the plugin code ?

I'm thinking of using MEF with LAZY collection of Import, but i need to instantiate them first to get my buttons informations. So second question

If i set the Import Collection to null and lauch the compose() function again, the plugins will be load or wait until call to be load (lazy) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-10-22 01:29:14

然后我想处理掉它们,直到它们被调用为止。

您应该检查导入 元数据。您可以通过导入 LazyLazy> 来获取此元数据。您可以使用 ExportMetadata 属性将此元数据添加到导出中。

首先关于 .NET:dotNet 是否仅保留对内存中 dll 插件的引用或所有插件代码?

一旦程序集被加载,它就会保留在内存中,除非您卸载整个 AppDomain。

此外,.NET 4.0 中没有现成的 ComposablePartCatalog 实现,可以在不加载相应程序集的情况下进行查询。但从理论上讲,如果您将元数据存储在程序集外部的某个位置,就可以完成类似的操作。 codeplex 上的 MEF 代码 中提供了此类实现的示例。

我正在考虑将 MEF 与 LAZY 导入集合一起使用

使用延迟导入不一定会阻止加载程序集。如果您有对 Lazy 对象的引用,则至少必须加载包含 IFoo 的程序集。正如我上面所解释的,包含导出的 IFoo 实现的程序集也将在此时加载。

使用 Lazy 只会推迟某些构造函数的调用,从而有望加快应用程序的启动速度。

Then i would like to dispose them until they are called.

Instead of inspecting imported objects and then throwing them away, you should inspect the import metadata. You can get this metadata by importing Lazy<IFoo,IFooMetadata> or Lazy<IFoo,Dictionary<string,object>>. You can add this metadata to exports with the ExportMetadata attribute.

So first about .NET : Does dotNet keep only a reference to the dll plugins in memory or all the plugin code ?

Once an assembly is loaded it remains in memory, unless you unload the whole AppDomain.

Also, there are no out-of-the-box implementations of ComposablePartCatalog in .NET 4.0 which can be queried without loading the corresponding assembly. But in theory something like that could be done if you store the metadata somewhere outside the assembly. There is a sample of such an implementation in the MEF code on codeplex.

I'm thinking of using MEF with LAZY collection of Import

Using lazy imports will not necessarily prevent assemblies from being loaded. If you have a reference to a Lazy<IFoo> object, then at least the assembly containing IFoo has to be loaded. And as I explained above, the assembly containing the exported IFoo implementation will also have been loaded at that point.

Using Lazy will only postpone the invocation of some constructors, hopefully resulting in faster start-up of your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文