问题描述
我了解 MEF CompositionContainer 创建并保留类的实例.我不知道在什么情况下 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.
- 任何人都可以列出在 CompositionContainer 或 CompositionContainer 类的方法上执行的导致 CompositionContainer 在 CompositionContainer 中存储类的实例的操作.
- 是否可以在调试器或任何其他方式中查看 CompositionContainer 中保存的类实例?
推荐答案
CompositionContainer 将在 CompositionContainer 的生命周期内保留对所有共享部件的引用.(导入和导出的默认 CreationPolicy 均为 Any,这意味着默认情况下,除非另有说明,否则所有部分都将共享.)
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.)
如果部件实现 IDisposable,则将保留对非共享部件的引用.当从容器中拉出的根导出被释放(如果该导出来自非共享部分)时,引用将被释放.可以通过调用 CompositionContainer.ReleaseExport 或 ExportLifetimeContext.Dispose 来释放导出,以使用 ExportFactory 创建的导出.
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.
我认为没有任何简单的方法可以查看 CompositionContainer 所持有的内容.源代码可用,因此您理论上可以深入研究它并找出它的确切存储位置.
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.
这篇关于mef 中组合容器持有的类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!