mscorlib(4.0.0.0) API with additions
WeakReference.cs
4 using System.Security;
6 
7 namespace System
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
13  [SecurityPermission(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
15  {
16  internal IntPtr m_handle;
17 
21  [__DynamicallyInvokable]
22  public virtual bool IsAlive
23  {
24  [MethodImpl(MethodImplOptions.InternalCall)]
25  [SecuritySafeCritical]
26  [__DynamicallyInvokable]
27  get;
28  }
29 
33  [__DynamicallyInvokable]
34  public virtual bool TrackResurrection
35  {
36  [__DynamicallyInvokable]
37  get
38  {
39  return IsTrackResurrection();
40  }
41  }
42 
47  [__DynamicallyInvokable]
48  public virtual object Target
49  {
50  [MethodImpl(MethodImplOptions.InternalCall)]
51  [SecuritySafeCritical]
52  [__DynamicallyInvokable]
53  get;
54  [MethodImpl(MethodImplOptions.InternalCall)]
55  [SecuritySafeCritical]
56  [__DynamicallyInvokable]
57  set;
58  }
59 
62  [__DynamicallyInvokable]
63  public WeakReference(object target)
64  : this(target, trackResurrection: false)
65  {
66  }
67 
71  [__DynamicallyInvokable]
72  public WeakReference(object target, bool trackResurrection)
73  {
74  Create(target, trackResurrection);
75  }
76 
83  {
84  if (info == null)
85  {
86  throw new ArgumentNullException("info");
87  }
88  object value = info.GetValue("TrackedObject", typeof(object));
89  bool boolean = info.GetBoolean("TrackResurrection");
90  Create(value, boolean);
91  }
92 
94  [MethodImpl(MethodImplOptions.InternalCall)]
95  [SecuritySafeCritical]
96  [__DynamicallyInvokable]
97  extern ~WeakReference();
98 
104  [SecurityCritical]
105  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
106  {
107  if (info == null)
108  {
109  throw new ArgumentNullException("info");
110  }
111  info.AddValue("TrackedObject", Target, typeof(object));
112  info.AddValue("TrackResurrection", IsTrackResurrection());
113  }
114 
115  [MethodImpl(MethodImplOptions.InternalCall)]
116  [SecuritySafeCritical]
117  private extern void Create(object target, bool trackResurrection);
118 
119  [MethodImpl(MethodImplOptions.InternalCall)]
120  [SecuritySafeCritical]
121  private extern bool IsTrackResurrection();
122  }
125  [Serializable]
126  [__DynamicallyInvokable]
127  public sealed class WeakReference<T> : ISerializable where T : class
128  {
129  internal IntPtr m_handle;
130 
131  private T Target
132  {
133  [MethodImpl(MethodImplOptions.InternalCall)]
134  [SecuritySafeCritical]
135  get;
136  [MethodImpl(MethodImplOptions.InternalCall)]
137  [SecuritySafeCritical]
138  set;
139  }
140 
143  [__DynamicallyInvokable]
144  public WeakReference(T target)
145  : this(target, trackResurrection: false)
146  {
147  }
148 
153  [__DynamicallyInvokable]
154  public WeakReference(T target, bool trackResurrection)
155  {
156  Create(target, trackResurrection);
157  }
158 
159  internal WeakReference(SerializationInfo info, StreamingContext context)
160  {
161  if (info == null)
162  {
163  throw new ArgumentNullException("info");
164  }
165  T target = (T)info.GetValue("TrackedObject", typeof(T));
166  bool boolean = info.GetBoolean("TrackResurrection");
167  Create(target, boolean);
168  }
169 
174  [MethodImpl(MethodImplOptions.AggressiveInlining)]
175  [__DynamicallyInvokable]
176  public bool TryGetTarget(out T target)
177  {
178  return (target = Target) != null;
179  }
180 
183  [__DynamicallyInvokable]
184  public void SetTarget(T target)
185  {
186  Target = target;
187  }
188 
190  [MethodImpl(MethodImplOptions.InternalCall)]
191  [SecuritySafeCritical]
192  extern ~WeakReference();
193 
199  [SecurityCritical]
201  {
202  if (info == null)
203  {
204  throw new ArgumentNullException("info");
205  }
206  info.AddValue("TrackedObject", Target, typeof(T));
207  info.AddValue("TrackResurrection", IsTrackResurrection());
208  }
209 
210  [MethodImpl(MethodImplOptions.InternalCall)]
211  [SecuritySafeCritical]
212  private extern void Create(T target, bool trackResurrection);
213 
214  [MethodImpl(MethodImplOptions.InternalCall)]
215  [SecuritySafeCritical]
216  private extern bool IsTrackResurrection();
217  }
218 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
WeakReference(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.WeakReference class, using deserialized data from the spec...
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo object with all the data necessary to se...
Definition: __Canon.cs:3
WeakReference(object target)
Initializes a new instance of the T:System.WeakReference class, referencing the specified object.
WeakReference(T target)
Initializes a new instance of the T:System.WeakReference`1 class that references the specified object...
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Represents a weak reference, which references an object while still allowing that object to be reclai...
SecurityAction
Specifies the security actions that can be performed using declarative security.
bool TryGetTarget(out T target)
Tries to retrieve the target object that is referenced by the current T:System.WeakReference`1 object...
WeakReference(T target, bool trackResurrection)
Initializes a new instance of the T:System.WeakReference`1 class that references the specified object...
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
void SetTarget(T target)
Sets the target object that is referenced by this T:System.WeakReference`1 object.
MethodImplOptions
Defines the details of how a method is implemented.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
WeakReference(object target, bool trackResurrection)
Initializes a new instance of the T:System.WeakReference class, referencing the specified object and ...
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo object with all the data needed to seria...
Specifies that the class can be serialized.
SecurityPermissionFlag
Specifies access flags for the security permission object.
virtual object Target
Gets or sets the object (the target) referenced by the current T:System.WeakReference object.