Testando / Verificando uma WeakReference
Gostaria de verificar se o código está configurando umWeakReference
acidentalmente não mantém uma forte referência ao objeto referenciado. (Aqui estáum exempl de como é fácil fazer isso acidentalmente.)
Parece a melhor maneira de verificar se há referências fortes inadvertidas?
TestObject testObj = new TestObject();
WeakReference wr = new WeakReference(testObj);
// Verify that the WeakReference actually points to the intended object instance.
Assert.Equals(wr.Target, testObject);
// Force disposal of testObj;
testObj = null;
GC.Collect();
// If no strong references are left to the wr.Target, wr.IsAlive will return false.
Assert.False(wr.IsAlive);