Armin,
Yes, but Dispose must be called manually, that's why I thought it's not what
the OP was looking for.
Its probably not what the OP is looking for! ;-) However, remember there is
no real deterministic finalization in .NET, and once the collection has a
"hard" reference to the object that the finalizer or the VB6 Terminate event
"cannot" occur, which is where the .NET WeakReference is rather cool! I
mentioned both (WeakReference & Dispose) as I would probable implement both.
I should add:The GC will set the reference in the WeakReference to nothing if the GC
needs to do a garbage collection, assuming there are no other references to
the object. If there is not garbage collection the WeakReference will
maintain the reference when there are no other references to the object...
Also WeakReference supports a long & short weak references, which allow an
object heading to the Finalization queue to be revived.
I have a project where I am implementing the domain model equivalent of the
DataTable & DataView classes, my plan is that my Domain collection will
maintain a weakreference to its default "View" object, this way when the
view object is in use it will stick around, when the View object is not in
use, it can will quietly disappear... And there is no real extra code in my
logic.
Note WeakReferences are also useful for semi-persistent Singletons. A
singleton that exists while other objects refer to it, however if no other
objects refer to it, and the GC needs memory the Singleton based on a
WeakReference can disappear... In this regard I've considered it in a
special plug-in architecture in a project I use to practice Refactoring
http://www.refactoring.com.
O.K. too much info ;-)
Hope this helps
Jay