mef 中的组合容器所持有的类的实例

发布于 2024-10-31 14:51:18 字数 293 浏览 1 评论 0原文

据我了解,MEF CompositionContainer 创建并保留类的实例。我不知道在什么情况下 CompositionContainer 的内部会有一个类实例。

  1. 任何人都可以列出对 CompositionContainer 执行的操作或 CompositionContainer 类的方法,这些操作导致 CompositionContainer 在 CompositionContainer 中存储类的实例。
  2. 是否可以在调试器或任何其他方式中查看 CompositionContainer 中保存的类实例?

I understand that a MEF CompositionContainer creates and keeps instances of classes. I don't know under what circumstances a CompositionContainer has a class instance in its bowels.

  1. Can anybody list operations performed on the CompositionContainer or methods of the CompositionContainer class that cause the CompositionContainer to store an instance of a class within the CompositionContainer.
  2. Is it possible to view class instances held within a CompositionContainer in the debugger or any other fashion?

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

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

发布评论

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

评论(2

小镇女孩 2024-11-07 14:51:18

CompositionContainer 将在 CompositionContainer 的生命周期内保留对所有共享部分的引用。 (对于导入和导出,默认的 CreationPolicy 都是 Any,这意味着默认情况下,除非另有指定,否则所有部件都将被共享。)

如果部件实现了 IDisposable,则将保留对 NonShared 部件的引用。当从容器中提取的根导出被释放时(如果该导出来自非共享部分),该引用将被释放。对于使用 ExportFactory 创建的导出,可以通过调用 CompositionContainer.ReleaseExport 或 ExportLifetimeContext.Dispose 来释放导出。

我认为没有任何简单的方法可以查看 CompositionContainer 所保存的内容。源代码是可用的,因此理论上您可以深入研究它并找出它的确切存储位置。

The CompositionContainer will keep references to all shared parts for the lifetime of the CompositionContainer. (The default CreationPolicy is Any for both imports and exports, which means by default all parts will be shared unless otherwise specified.)

References to NonShared parts will be kept if the part implements IDisposable. The reference will be released when the root export that was pulled from the container is released (if that export was from a NonShared part). Exports can be released either by calling CompositionContainer.ReleaseExport, or ExportLifetimeContext.Dispose for exports created with an ExportFactory.

I don't think there's any simple way to view what's held by the CompositionContainer. The source code is available so you could theoretically dive into it and figure out exactly where it's stored.

小鸟爱天空丶 2024-11-07 14:51:18

关于你的第二个问题(上面#2)...

使用 QuickWatch 窗口 (Shift + F9) 或常规 Watch 窗口,将以下内容复制到其中:
((System.ComponentModel.Composition.Hosting.CompositionContainer)(this.Container))._catalogExportProvider._activatedParts

上面的行假设您停止的对象具有“this.Container”属性,该属性是您所引用范围的CompositionContainer

从那里,您将获得一系列ActivatedParts。然后您可以浏览零件字典。找到您想要查找其实例的零件定义,然后展开其“非公共成员”。在那里您将找到CachedInstance,这将是已创建的“共享”导出部件的实例。

我认为导出 NonShared 而不是 IDisposable 的部分根本不会被缓存或保留。至少这是我见过的行为。

快速观看

In regards to your second question (#2 above) ...

Using the QuickWatch Window (Shift + F9) or a regular Watch window, copy the following in there:
((System.ComponentModel.Composition.Hosting.CompositionContainer)(this.Container))._catalogExportProvider._activatedParts

The line above assumes that the object you are stopped on has a "this.Container" property, which is the CompositionContainer of the scope you are referring to.

From there, you'll get an array of ActivatedParts. You then navigate the dictionary of Parts. Find the Part Definition you want to find the instance of, and expand its "Non-Public Members". There you will find the CachedInstance, and this will be the instance of your "Shared" exported part that's been created.

I think Parts that are exported NonShared and not IDisposable aren't cached or held onto at all. At least that's the behavior I've seen.

Quick Watch

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