I wanted to do this in order to test my code. I suppose I could make another wrapper around the weakreference object, but would prefer to work directly to simulate the original object getting reclaimed.
This is the code that I have so far
var myString = "someString";
var myStringRef = new WeakReference(myString);
myString = null;
GC.Collect();
Assert.IsFalse(myStringRef.IsAlive) //I want this to pass, but it failed. IsAlive is still true.
What can I do to force the last line above to not fail (ie. !IsAlive
)
I have some framework logic that does housekeeping based on the status of the weakreference. I want to be able to test that logic, to do so, I need to be able to change the weakreference state at will - so how can I do this?