Я хотел бы проверить, что код, устанавливающий WeakReference
, случайно не содержит ссылки на ссылочный объект. (Здесь пример о том, как легко случайно это сделать.)
Означает ли это, что это лучший способ проверить ненадежные ссылки?
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);