Wed Sep 11 15:26:34 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System / WeakReference.cs
1 //
2 // System.WeakReference.cs
3 //
4 // Author:
5 //   Ajay kumar Dwivedi (adwiv@yahoo.com)
6 //
7
8 using System.Runtime.Serialization;
9 using System.Runtime.InteropServices;
10
11 namespace System
12 {
13         /// <summary>
14         /// Summary description for WeakReference.
15         /// </summary>
16         [Serializable]
17         public class WeakReference : ISerializable
18         {
19                 //Fields
20                 private bool isLongReference;
21                 private GCHandle gcHandle;
22
23                 // Helper method for constructors
24                 //Should not be called from any other method.
25                 private void AllocateHandle(Object target)
26                 {
27                         if(this.isLongReference)
28                         {
29                                 this.gcHandle = GCHandle.Alloc(target, GCHandleType.WeakTrackResurrection);
30                         }
31                         else
32                         {
33                                 this.gcHandle = GCHandle.Alloc(target, GCHandleType.Weak);
34                         }
35                 }               
36                 
37                 
38                 //Constructors
39                 public WeakReference(object target)
40                         : this(target,false)
41                 {}
42
43                 
44                 public WeakReference(object target, bool trackResurrection)
45                 {
46                         this.isLongReference = trackResurrection;
47                         AllocateHandle(target);
48                 }
49
50                 
51                 protected WeakReference(SerializationInfo info, StreamingContext context)
52                 {
53                         if (info == null)
54                                 throw new ArgumentNullException ("info");
55
56                         this.isLongReference = info.GetBoolean ("TrackResurrection");
57                         Object target = info.GetValue ("TrackedObject", typeof (System.Object));
58
59                         AllocateHandle(target);
60                 }
61
62                 
63                 // Properties
64                 public virtual bool IsAlive 
65                 {
66                         get
67                         {
68                                 //Target property takes care of the exception
69                                 return (Target != null);                
70                         }
71                 }
72
73                 public virtual object Target 
74                 {
75                         get
76                         {
77                                 //Exception is thrown by gcHandle's Target
78                                 return this.gcHandle.Target;
79                         }
80                         set
81                         {
82                                 this.gcHandle.Target = value;
83                         }
84                 }
85
86                 public virtual bool TrackResurrection 
87                 {
88                         get
89                         {
90                                 return this.isLongReference;
91                         }
92                 }
93
94                 //Methods
95                 ~WeakReference()
96                 {
97                         gcHandle.Free();
98                 }
99
100                 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
101                 {
102                         if (info == null)
103                                 throw new ArgumentNullException ("info");
104
105                         info.AddValue ("TrackResurrection", TrackResurrection);
106                         
107                         try {
108                                 info.AddValue ("TrackedObject", Target);
109                         } catch(Exception) {
110                                 info.AddValue("TrackedObject",null);
111                         }
112                 }
113         }
114 }