Click here to Skip to main content
Click here to Skip to main content

Generic WeakReference

By , 22 Dec 2007
 

Introduction

In the .NET Framework, there is no generic implementation of the System.WeakReference class. This means that runtime casting has to be done by the developer which is error prone and repetitive work. I did this simple implementation to rid this problem. Of course, it does not help with the overhead that boxing introduces since it's still stored in an object variable in the base class. Hopefully future releases of the framework will contain a real generic WeakReference.

Using the Code

Just cut the code snippet below and paste it into your project to use it.

    /// <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 the object that is referenced.</typeparam>
    [Serializable]
    public class WeakReference<T>
        : WeakReference where T : class
    {
        /// <summary>
        /// Initializes a new instance of the WeakReference{T} class, referencing
        /// the specified object.
        /// </summary>
        /// <param name="target">The object to reference.</param>
        public WeakReference(T target)
            : base(target)
        { }
        /// <summary>
        /// Initializes a new instance of the 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 true, the object is tracked
        /// after finalization; if false, the object is only tracked 
        /// until finalization.</param>
        public WeakReference(T target, bool trackResurrection)
            : base(target, trackResurrection)
        { }
        protected WeakReference(SerializationInfo info, StreamingContext context)
            : base(info, context)
        { }
        /// <summary>
        /// Gets or sets the object (the target) referenced by the 
        /// current WeakReference{T} object.
        /// </summary>
        public new T Target
        {
            get
            {
                return (T)base.Target;
            }
           set
            {
                base.Target = value;
            }
        }
    }        

History

  • 21st December, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RexNebular
Sweden Sweden
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Layout  Per page   
GeneralNice workmemberDavid McMinn29 Aug '08 - 5:41 
Mainly because it saves me having to do it Smile | :)
 
One thing though:
 
"Of course, it does not help with the overhead that boxing introduces since it's still stored in an object variable"
 
Surely it's not boxed, since boxing is for storing value types as objects. Classes derive from object so only a cast is necessary in the .NET WeakReference?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 22 Dec 2007
Article Copyright 2007 by RexNebular
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid