I'm experimenting with WeakReference, and I'm writing a code that checks if a weak reference is valid before returning a strong reference to the object.
if (weakRef.IsValid)
return (ReferencedType)weakRef.Target;
else
// Build a new object
How should I prevent the GC collecting the object between the "IsValid" and the "Target" calls?