using System; using System.Runtime.Serialization; /// /// Represents a weak reference, which references an object while still allowing /// that object to be reclaimed by garbage collection. /// /// The type of the object that is referenced. namespace Svelto.DataStructures { [Serializable] public class WeakReference : WeakReference where T : class { public bool IsValid { get { return Target != null && IsAlive == true; } } /// /// Gets or sets the object (the target) referenced by the /// current WeakReference{T} object. /// public new T Target { get { return (T)base.Target; } set { base.Target = value; } } /// /// Initializes a new instance of the WeakReference{T} class, referencing /// the specified object. /// /// The object to reference. public WeakReference(T target) : base(target) { } /// /// Initializes a new instance of the WeakReference{T} class, referencing /// the specified object and using the specified resurrection tracking. /// /// An object to track. /// Indicates when to stop tracking the object. /// If true, the object is tracked /// after finalization; if false, the object is only tracked /// until finalization. public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection) { } protected WeakReference(SerializationInfo info, StreamingContext context) : base(info, context) { } } public static class WeakReferenceUtility { public static bool IsValid(this WeakReference obj) { return obj != null && obj.IsAlive == true && obj.Target != null; } } }