返回顶部

收藏

通用WeakReference类

更多
/// <summary>
/// Represents a weak reference, which references an object while still
/// allowing that object to be garbage collected.
/// </summary>
/// <typeparam name="T">
/// The type of <see cref="Target"/>
/// </typeparam>
public class WeakReference<T> : System.WeakReference
{
    /// <summary>
    /// Initializes a new instance of the <see cref="WeakReference{T}"/> class,
    /// referencing the specified object.
    /// </summary>
    /// <param name="target">
    /// The object to track or <see langword="null"/>. 
    /// </param>
    public WeakReference(T target) : base(target)
    { }

    /// <summary>
    /// Initializes a new instance of the <see cref="WeakReference{T}"/> class,
    /// referencing the specified object and using the specified
    /// resurrection tracking.
    /// </summary>
    /// <param name="target">An object to track. 
    /// </param>
    /// <param name="trackResurrection">Indicates when to stop tracking the
    /// object. If <see langword="true"/>, the object is tracked after
    /// finalization; if <see langword="false"/>, the object is only tracked
    /// until finalization. 
    /// </param>
    public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection)
    { }

    /// <summary>
    /// Initializes a new instance of the <see cref="WeakReference"/> class,
    /// using deserialized data from the specified serialization and stream
    /// objects.
    /// </summary>
    /// <param name="info">
    /// An object that holds all the data needed to serialize or deserialize
    /// the current <see cref="T:System.WeakReference"/> object. 
    /// </param>
    /// <param name="context">(Reserved) Describes the source and
    /// destination of the serialized stream specified by 
    /// <paramref name="info"/>. 
    /// </param>
    /// <exception cref="ArgumentNullException"><paramref name="info"/> is 
    /// <see langword="null"/>. 
    /// </exception>
    protected WeakReference(SerializationInfo info, StreamingContext context) 
        : base(info, context)
    { }

    public new T Target
    {
        get { return (T)base.Target;}
        set { base.Target = value; }
    }
}
//该片段来自于http://outofmemory.cn

标签:c#,.net,基础

收藏

0人收藏

支持

0

反对

0

发表评论