Home
Manage Your Code
Snippet: Generic WeakReference class (C#)
Title: Generic WeakReference class Language: C#
Description: Wraps the System.WeakReference class with a generic interface. Views: 164
Author: Stephen Smith Date Added: 3/8/2010
Copy Code  
1/// <summary>

2/// Represents a weak reference, which references an object while still

3/// allowing that object to be garbage collected.

4/// </summary>

5/// <typeparam name="T">

6/// The type of <see cref="Target"/>

7/// </typeparam>

8public class WeakReference<T> : System.WeakReference
9{
10    /// <summary>

11    /// Initializes a new instance of the <see cref="WeakReference"/> class,

12    /// referencing the specified object.

13    /// </summary>

14    /// <param name="target">

15    /// The object to track or <see langword="null"/>. 

16    /// </param>

17    public WeakReference(T target) : base(target)
18    { }
19
20    /// <summary>

21    /// Initializes a new instance of the <see cref="WeakReference"/> class,

22    /// referencing the specified object and using the specified

23    /// resurrection tracking.

24    /// </summary>

25    /// <param name="target">An object to track. 

26    /// </param>

27    /// <param name="trackResurrection">Indicates when to stop tracking the

28    /// object. If <see langword="true"/>, the object is tracked after

29    /// finalization; if <see langword="false"/>, the object is only tracked

30    /// until finalization. 

31    /// </param>

32    public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection)
33    { }
34
35    /// <summary>

36    /// Initializes a new instance of the <see cref="WeakReference"/> class,

37    /// using deserialized data from the specified serialization and stream

38    /// objects.

39    /// </summary>

40    /// <param name="info">

41    /// An object that holds all the data needed to serialize or deserialize

42    /// the current <see cref="T:System.WeakReference"/> object. 

43    /// </param>

44    /// <param name="context">(Reserved) Describes the source and

45    /// destination of the serialized stream specified by 

46    /// <paramref name="info"/>. 

47    /// </param>

48    /// <exception cref="ArgumentNullException"><paramref name="info"/> is 

49    /// <see langword="null"/>. 

50    /// </exception>

51    protected WeakReference(SerializationInfo info, StreamingContext context) 
52        : base(info, context)
53    { }
54
55    public new T Target
56    {
57        get { return (T)base.Target;}
58        set { base.Target = value; }
59    }
60}
61
Usage
Same as per System.WeakReference