I\'d like to verify that code setting up a WeakReference does not accidentally hold a strong reference to the referenced object. (Here\'s an example of how it is easy to acciden
I did this just yesterday. Here's what I had to add to ensure the collection happened prior to your last assert:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.WaitForFullGCComplete();
GC.Collect();
If after this .IsAlive is still true, it's likely there is still a strong reference somewhere.
Incidentally - Be sure to NOT check .IsAlive when you access your WeakReference target. To avoid a race condition between you checking .IsAlive and .Target, do this:
var r = weakRef.Target AS Something;
if (r != null)
{
... do your thing
}