Feedback

C# - Generische WeakReference

Veröffentlicht von am 1/4/2010
(2 Bewertungen)
Generische Ableitung der WeakReference
 /// <summary>
    /// StrongTypes WeakReference
    /// </summary>
    /// <typeparam name="T">Type of the Target for which the Weakrefernce should be stored.</typeparam>
    public sealed class WeakReference<T>
    {
        WeakReference m_Target;



        /// <summary>
        /// The Strong Typed Reference Target
        /// </summary>
        public T Target
        {
            get
            {
                T result;

                result = (T)m_Target.Target;

                return result;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is alive.
        /// </summary>
        /// <value><c>true</c> if this instance is alive; otherwise, <c>false</c>.</value>
        public bool IsAlive
        {
            get 
            {
                bool alive = false;

                if (m_Target != null)
                {
                    alive = m_Target.IsAlive;
                }
                return alive;
            }
        }

        /// <summary>
        /// Gets a value indicating whether [track resurrection].
        /// </summary>
        /// <value><c>true</c> if [track resurrection]; otherwise, <c>false</c>.</value>
        public bool TrackResurrection
        {
            get
            {
                bool track = false;

                if (m_Target != null)
                {
                    track = m_Target.TrackResurrection;
                }

                return track;

            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WeakReference&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="target">The target.</param>
        public WeakReference(T target)
        {
            Contract.Requires(target != null, "The Target should be different from null!");
            this.m_Target = new WeakReference(target);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WeakReference&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="trackResurrection">Indicates wether the object will be tracked after resurrection</param>
        public WeakReference(T target, bool trackResurrection)
        {
            Contract.Requires(target != null, "The Target should be different from null!");
            this.m_Target = new WeakReference(target,trackResurrection);
        }

    }

3 Kommentare zum Snippet

jack schrieb am 3/4/2010:
Worum geht es hier genau?
Rainer Hilmer schrieb am 3/4/2010:
Ich verstehe auch nicht so recht was das bringen soll. Ausser daß das Ding Code Contracts implementiert, wird doch alles nur 1:1 gekapselt und weitergeleitet. Aber vielleicht schnall ich das ja nur nicht.
Thomas Söhne schrieb am 3/9/2010:
Eine typisierte Weakreferenz, das erspart das type-casten.
 

Logge dich ein, um hier zu kommentieren!