Code Snippets
Title: WeakReference target
Description: Generic WeakReference target.
Category: WeakReference
Visibility: Public
Added by: ohanlonp on 5/6/2012
Currently rated 5 by 1 user(s)
Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
using System;
using System.Runtime.InteropServices;
namespace $namespace$
{
/// <summary>
/// Represents a weak reference, which references an object while still allowing that
/// object to be reclaimed by garbage collection.
/// </summary>
/// <typeparam name="T">The type of object that is weak referenced.</typeparam>
/// <remarks>
/// I'd like to thank <see href="http://sachabarber.net">Sacha Barber</see> for providing
/// this class.
/// </remarks>
internal class WeakReference<T> : IDisposable
{
#region Methods
private GCHandle _handle;
private bool _trackResurrection;
#endregion
/// <summary>
/// Instantiate a new instance of the <see cref="WeakReference"/> class.
/// </summary>
/// <param name="target">The object to track.</param>
public WeakReference(T target) : this(target, false) { }
/// <summary>
/// Instantiate a new instance of the <see cref="WeakReference"/> class.
/// </summary>
/// <param name="target">The object to track.</param>
/// <param name="trackResurrection">Indicates when to stop tracking the object.
/// If true, the object is tracked after finalization; if false, the object is
/// only tracked until finalization.</param>
/// <remarks>If trackResurrection is false, a short weak reference is created.
/// If trackResurrection is true, a long weak reference is created.</remarks>
public WeakReference(T target, bool trackResurrection)
{
this._trackResurrection = trackResurrection;
this.Target = target;
}
/// <summary>
/// Finalizer to <see cref="Dispose"/> of the objects.
/// </summary>
~WeakReference()
{
Dispose();
}
/// <summary>
/// Dispose of the <see cref="WeakReference"/>.
/// </summary>
public void Dispose()
{
_handle.Free();
GC.SuppressFinalize(this);
}
/// <summary>
/// Gets an indication whether the object referenced by
/// the current WeakReference object has been garbage collected
/// </summary>
public virtual bool IsAlive
{
get
{
return (_handle.Target != null);
}
}
/// <summary>
/// Gets an indication whether the object referenced by the current WeakReference object is
/// tracked after it is finalized.
/// </summary>
public virtual bool TrackResurrection
{
get
{
return this._trackResurrection;
}
}
/// <summary>
/// Gets or sets the object (the target) referenced by the current WeakReference object.
/// </summary>
public virtual T Target
{
get
{
object o = _handle.Target;
if ((o == null) || (!(o is T)))
return default(T);
return (T)o;
}
set
{
_handle = GCHandle.Alloc(value,
this._trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
}
}
#region Implicit methods
public static implicit operator WeakReference<T>(T obj)
{
return new WeakReference<T>(obj);
}
public static implicit operator T(WeakReference<T> weakRef)
{
return weakRef.Target;
}
#endregion
}
}