[runtime] Fix Empty generic enumerator equality
[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         [ComVisible (true)]
38         public class WeakReference : ISerializable
39         {
40                 //Fields
41                 private bool isLongReference;
42                 private GCHandle gcHandle;
43
44                 // Helper method for constructors
45                 //Should not be called from any other method.
46                 private void AllocateHandle (Object target)
47                 {
48                         if (isLongReference) {
49                                 gcHandle = GCHandle.Alloc (target, GCHandleType.WeakTrackResurrection);
50                         }
51                         else {
52                                 gcHandle = GCHandle.Alloc (target, GCHandleType.Weak);
53                         }
54                 }
55
56                 //Constructors
57 #if MOBILE
58                 protected WeakReference ()
59                 {
60                 }
61 #endif
62                 public WeakReference (object target)
63                         : this (target, false)
64                 {
65                 }
66
67                 public WeakReference (object target, bool trackResurrection)
68                 {
69                         isLongReference = trackResurrection;
70                         AllocateHandle (target);
71                 }
72
73                 protected WeakReference (SerializationInfo info, StreamingContext context)
74                 {
75                         if (info == null)
76                                 throw new ArgumentNullException ("info");
77
78                         isLongReference = info.GetBoolean ("TrackResurrection");
79                         Object target = info.GetValue ("TrackedObject", typeof (System.Object));
80
81                         AllocateHandle (target);
82                 }
83
84                 // Properties
85                 public virtual bool IsAlive {
86                         get {
87                                 //Target property takes care of the exception
88                                 return (Target != null);
89                         }
90                 }
91
92                 public virtual object Target {
93                         get {
94                                 // This shouldn't throw an exception after finalization
95                                 // http://blogs.msdn.com/b/yunjin/archive/2005/08/31/458231.aspx
96                                 if (!gcHandle.IsAllocated)
97                                         return null;
98                                 return gcHandle.Target;
99                         }
100                         set
101                         {
102                                 gcHandle.Target = value;
103                         }
104                 }
105
106                 public virtual bool TrackResurrection {
107                         get {
108                                 return isLongReference;
109                         }
110                 }
111
112                 //Methods
113                 ~WeakReference ()
114                 {
115                         gcHandle.Free ();
116                 }
117
118                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
119                 {
120                         if (info == null)
121                                 throw new ArgumentNullException ("info");
122
123                         info.AddValue ("TrackResurrection", TrackResurrection);
124
125                         try {
126                                 info.AddValue ("TrackedObject", Target);
127                         } catch (Exception) {
128                                 info.AddValue ("TrackedObject", null);
129                         }
130                 }
131         }
132 }