Show / Hide Table of Contents

Class WeakReferenceExtensions

Provides a set of extension methods of System.WeakReference<>.

Inheritance
System.Object
WeakReferenceExtensions
Namespace: System
Assembly: Nuqleon.Memory.dll
Syntax
public static class WeakReferenceExtensions : Object

Methods

Create<T>(T)

Creates a new weak reference to the specified target object.

Declaration
public static WeakReference<T> Create<T>(T target)
    where T : class
Parameters
Type Name Description
T target

The target object referred to by the weak reference.

Returns
Type Description
System.WeakReference<T>

A new weak reference referring to the specified target object.

Type Parameters
Name Description
T

Type of the object referred to by the weak reference. This type has to be a reference type.

Remarks

Weak reference instances created using this method use a singleton instance for a weak reference containing a null reference. The GetTarget<T>(WeakReference<T>) and GetOrSetTarget<T>(WeakReference<T>, Func<T>) methods use this singleton instance to differentiate between an intentional (weak) null reference and a weak reference whose target has been collected.

Create<T>(T, Boolean)

Creates a new weak reference to the specified target object, using the specified resurrection tracking.

Declaration
public static WeakReference<T> Create<T>(T target, bool trackResurrection)
    where T : class
Parameters
Type Name Description
T target

The target object referred to by the weak reference.

System.Boolean trackResurrection

true to track the object after finalization; false to track the object only until finalization.

Returns
Type Description
System.WeakReference<T>

A new weak reference referring to the specified target object.

Type Parameters
Name Description
T

Type of the object referred to by the weak reference. This type has to be a reference type.

Remarks

Weak reference instances created using this method use a singleton instance for a weak reference containing a null reference. The GetTarget<T>(WeakReference<T>) and GetOrSetTarget<T>(WeakReference<T>, Func<T>) methods use this singleton instance to differentiate between an intentional (weak) null reference and a weak reference whose target has been collected.

GetOrSetTarget<T>(WeakReference<T>, Func<T>)

Gets the target object referred to by the specified weak reference. If the weak reference no longer has a reference to the underlying object, the specified factory delegate is invoked to create and set a new target.

Declaration
public static T GetOrSetTarget<T>(this WeakReference<T> weakReference, Func<T> targetFactory)
    where T : class
Parameters
Type Name Description
System.WeakReference<T> weakReference

The weak reference whose referred target object to obtain.

System.Func<T> targetFactory

The target factory delegate to invoke in case the weak reference no longer has a reference to the underlying object.

Returns
Type Description
T

The target object referred to by the weak reference.

Type Parameters
Name Description
T

Type of the object referred to by the weak reference. This type has to be a reference type.

Remarks

If the weak reference intentionally contains a null reference (see Create<T>(T)), the factory delegate won't get invoked and a null reference will be returned.

GetTarget<T>(WeakReference<T>)

Gets the target object referred to by the specified weak reference. If the weak reference no longer has a reference to the underlying object, a System.InvalidOperationException is thrown.

Declaration
public static T GetTarget<T>(this WeakReference<T> weakReference)
    where T : class
Parameters
Type Name Description
System.WeakReference<T> weakReference

The weak reference whose referred target object to obtain.

Returns
Type Description
T

The target object referred to by the weak reference.

Type Parameters
Name Description
T

Type of the object referred to by the weak reference. This type has to be a reference type.

Remarks

If the weak reference intentionally contains a null reference (see Create<T>(T)), a null reference will be returned.

Exceptions
Type Condition
System.InvalidOperationException

Thrown if the weak reference no longer holds a reference to the original target object.

In This Article
Back to top Generated by DocFX