New test.
[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 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Runtime.Serialization;
32 using System.Runtime.InteropServices;
33
34 namespace System
35 {
36         [Serializable]
37         public class WeakReference : ISerializable
38         {
39                 //Fields
40                 private bool isLongReference;
41                 private GCHandle gcHandle;
42
43                 // Helper method for constructors
44                 //Should not be called from any other method.
45                 private void AllocateHandle (Object target)
46                 {
47                         if (isLongReference) {
48                                 gcHandle = GCHandle.Alloc (target, GCHandleType.WeakTrackResurrection);
49                         }
50                         else {
51                                 gcHandle = GCHandle.Alloc (target, GCHandleType.Weak);
52                         }
53                 }
54
55                 //Constructors
56                 public WeakReference (object target)
57                         : this (target, false)
58                 {
59                 }
60
61                 public WeakReference (object target, bool trackResurrection)
62                 {
63                         isLongReference = trackResurrection;
64                         AllocateHandle (target);
65                 }
66
67                 protected WeakReference (SerializationInfo info, StreamingContext context)
68                 {
69                         if (info == null)
70                                 throw new ArgumentNullException ("info");
71
72                         isLongReference = info.GetBoolean ("TrackResurrection");
73                         Object target = info.GetValue ("TrackedObject", typeof (System.Object));
74
75                         AllocateHandle (target);
76                 }
77
78                 // Properties
79                 public virtual bool IsAlive {
80                         get {
81                                 //Target property takes care of the exception
82                                 return (Target != null);                
83                         }
84                 }
85
86                 public virtual object Target {
87                         get {
88                                 //Exception is thrown by gcHandle's Target
89                                 return gcHandle.Target;
90                         }
91                         set
92                         {
93                                 gcHandle.Target = value;
94                         }
95                 }
96
97                 public virtual bool TrackResurrection {
98                         get {
99                                 return isLongReference;
100                         }
101                 }
102
103                 //Methods
104                 ~WeakReference ()
105                 {
106                         gcHandle.Free ();
107                 }
108
109                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
110                 {
111                         if (info == null)
112                                 throw new ArgumentNullException ("info");
113
114                         info.AddValue ("TrackResurrection", TrackResurrection);
115
116                         try {
117                                 info.AddValue ("TrackedObject", Target);
118                         } catch (Exception) {
119                                 info.AddValue ("TrackedObject", null);
120                         }
121                 }
122         }
123 }