public static ReferenceCountedDisposable <T> Create <TDerived>(TDerived instance, out ReferenceCountedDisposable <TDerived> .WeakReference derivedWeakReference) where TDerived : class, IDisposable, T { var baseStrongRef = new ReferenceCountedDisposable <T>(instance); derivedWeakReference = new ReferenceCountedDisposable <TDerived> .WeakReference(instance, baseStrongRef._boxedReferenceCount); return(baseStrongRef); }
public WeakReference(ReferenceCountedDisposable <T> reference) : this() { if (reference == null) { throw new ArgumentNullException(nameof(reference)); } T?instance = reference._instance; StrongBox <int> referenceCount = reference._boxedReferenceCount; if (instance == null) { // The specified reference is already not valid. This case is supported by WeakReference (not // unlike `new System.WeakReference(null)`), but we return early to avoid an unnecessary // allocation in this case. return; } _weakInstance = new WeakReference <T>(instance); _boxedReferenceCount = referenceCount; }