物件破坏问题,MEF [英] Object destruction problem with MEF

查看:156
本文介绍了物件破坏问题,MEF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个静态变量持有对象的数量。在构造函数中增加我这个变量。这样,我知道如何创建对象的多个实例。
使用对象后,它们被解除引用阔叶。
我怀疑,如果 MEF 持有这些对象的引用,所以我强迫 GC 做一个干净的达(使用 GC.Collect的()方法)。我希望在下次创建对象从零这个变量的开端,但它最后一个数字继续。我把 A记录机制进行跟踪,并在应用程序关闭后,对象才被销毁。
我可以假设 MEF 创建其他引用这些对象?

I use a static variable for holding the count of objects. In constructor I increase this variable. This way I know how many instances of the object are created. After using the objects, they are leaved dereferenced. I'm doubtful if MEF is holding references to these objects so I force GC to do a clean up (Using GC.Collect() method). I expect on next object creation this variable start from zero but it resumes from last number. I put a logging mechanism in destructor for tracing, and objects are destroyed only after the application is closed. Can I assume MEF has created other references to those objects ?

我用 MEF ExportFactory 创建我的对象

编辑:

也许与 ExportLifetimeContext 的东西应该怎么办?

Maybe something with ExportLifetimeContext should be done ?

推荐答案

MEF目前拥有支持 ExportFactory 一个桌面版本。虽然名字有'出口'字,你应该实现它,你在做你的'进口'。到 CreateExport()方法的调用来创建你的一部分的新实例会返回一个 ExportLifetimeContext< T> 的这对象有一个的Dispose()的方法,可后来释放出口对象使用。这种方法会打电话给你的对象的Dispose()自动您不必手动调用它。

MEF now has a desktop version which supports ExportFactory. Although the name has 'export' word, you should implement it where you are doing your 'imports'. A call to CreateExport() method for creating a new instance of your part will return an ExportLifetimeContext<T> an this object has a Dispose() method which could be used later for releasing exported object. This method would call your object Dispose() automatically and you don't need to call it manually.

这行为是因为容器本身,甚至对这些对象的引用调用它们的的Dispose()对他们没有影响。

This behavior is because the container is the owner of created objects by itself and even with a reference to these objects calling their Dispose() has no effect on them.

这篇关于物件破坏问题,MEF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆