150 lines
6.4 KiB
C#
150 lines
6.4 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.Serialization;
|
|
|
|
namespace System
|
|
{
|
|
/// <summary>Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.</summary>
|
|
/// <filterpriority>2</filterpriority>
|
|
// Token: 0x020006C1 RID: 1729
|
|
[ComVisible(true)]
|
|
[Serializable]
|
|
public class WeakReference : ISerializable
|
|
{
|
|
// Token: 0x06004136 RID: 16694 RVA: 0x000DE574 File Offset: 0x000DC774
|
|
protected WeakReference()
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.WeakReference" /> class, referencing the specified object.</summary>
|
|
/// <param name="target">The object to track or null. </param>
|
|
// Token: 0x06004137 RID: 16695 RVA: 0x000DE57C File Offset: 0x000DC77C
|
|
public WeakReference(object target)
|
|
: this(target, false)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.WeakReference" /> class, referencing the specified object and using the specified resurrection tracking.</summary>
|
|
/// <param name="target">An object to track. </param>
|
|
/// <param name="trackResurrection">Indicates when to stop tracking the object. If true, the object is tracked after finalization; if false, the object is only tracked until finalization. </param>
|
|
// Token: 0x06004138 RID: 16696 RVA: 0x000DE588 File Offset: 0x000DC788
|
|
public WeakReference(object target, bool trackResurrection)
|
|
{
|
|
this.isLongReference = trackResurrection;
|
|
this.AllocateHandle(target);
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.WeakReference" /> class, using deserialized data from the specified serialization and stream objects.</summary>
|
|
/// <param name="info">An object that holds all the data needed to serialize or deserialize the current <see cref="T:System.WeakReference" /> object. </param>
|
|
/// <param name="context">(Reserved) Describes the source and destination of the serialized stream specified by <paramref name="info" />. </param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="info" /> is null. </exception>
|
|
// Token: 0x06004139 RID: 16697 RVA: 0x000DE5A0 File Offset: 0x000DC7A0
|
|
protected WeakReference(SerializationInfo info, StreamingContext context)
|
|
{
|
|
if (info == null)
|
|
{
|
|
throw new ArgumentNullException("info");
|
|
}
|
|
this.isLongReference = info.GetBoolean("TrackResurrection");
|
|
object value = info.GetValue("TrackedObject", typeof(object));
|
|
this.AllocateHandle(value);
|
|
}
|
|
|
|
// Token: 0x0600413A RID: 16698 RVA: 0x000DE5F4 File Offset: 0x000DC7F4
|
|
private void AllocateHandle(object target)
|
|
{
|
|
if (this.isLongReference)
|
|
{
|
|
this.gcHandle = GCHandle.Alloc(target, GCHandleType.WeakTrackResurrection);
|
|
}
|
|
else
|
|
{
|
|
this.gcHandle = GCHandle.Alloc(target, GCHandleType.Weak);
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets an indication whether the object referenced by the current <see cref="T:System.WeakReference" /> object has been garbage collected.</summary>
|
|
/// <returns>true if the object referenced by the current <see cref="T:System.WeakReference" /> object has not been garbage collected and is still accessible; otherwise, false.</returns>
|
|
/// <filterpriority>2</filterpriority>
|
|
// Token: 0x17000C0A RID: 3082
|
|
// (get) Token: 0x0600413B RID: 16699 RVA: 0x000DE62C File Offset: 0x000DC82C
|
|
public virtual bool IsAlive
|
|
{
|
|
get
|
|
{
|
|
return this.Target != null;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets or sets the object (the target) referenced by the current <see cref="T:System.WeakReference" /> object.</summary>
|
|
/// <returns>null if the object referenced by the current <see cref="T:System.WeakReference" /> object has been garbage collected; otherwise, a reference to the object referenced by the current <see cref="T:System.WeakReference" /> object.</returns>
|
|
/// <exception cref="T:System.InvalidOperationException">The reference to the target object is invalid. This exception can be thrown while setting this property if the value is a null reference or if the object has been finalized during the set operation.</exception>
|
|
/// <filterpriority>2</filterpriority>
|
|
// Token: 0x17000C0B RID: 3083
|
|
// (get) Token: 0x0600413C RID: 16700 RVA: 0x000DE63C File Offset: 0x000DC83C
|
|
// (set) Token: 0x0600413D RID: 16701 RVA: 0x000DE64C File Offset: 0x000DC84C
|
|
public virtual object Target
|
|
{
|
|
get
|
|
{
|
|
return this.gcHandle.Target;
|
|
}
|
|
set
|
|
{
|
|
this.gcHandle.Target = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets an indication whether the object referenced by the current <see cref="T:System.WeakReference" /> object is tracked after it is finalized.</summary>
|
|
/// <returns>true if the object the current <see cref="T:System.WeakReference" /> object refers to is tracked after finalization; or false if the object is only tracked until finalization.</returns>
|
|
/// <filterpriority>2</filterpriority>
|
|
// Token: 0x17000C0C RID: 3084
|
|
// (get) Token: 0x0600413E RID: 16702 RVA: 0x000DE65C File Offset: 0x000DC85C
|
|
public virtual bool TrackResurrection
|
|
{
|
|
get
|
|
{
|
|
return this.isLongReference;
|
|
}
|
|
}
|
|
|
|
/// <summary>Discards the reference to the target represented by the current <see cref="T:System.WeakReference" /> object.</summary>
|
|
// Token: 0x0600413F RID: 16703 RVA: 0x000DE664 File Offset: 0x000DC864
|
|
~WeakReference()
|
|
{
|
|
this.gcHandle.Free();
|
|
}
|
|
|
|
/// <summary>Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object with all the data needed to serialize the current <see cref="T:System.WeakReference" /> object.</summary>
|
|
/// <param name="info">An object that holds all the data needed to serialize or deserialize the current <see cref="T:System.WeakReference" /> object. </param>
|
|
/// <param name="context">(Reserved) The location where serialized data is stored and retrieved. </param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="info" /> is null. </exception>
|
|
/// <filterpriority>2</filterpriority>
|
|
// Token: 0x06004140 RID: 16704 RVA: 0x000DE6A4 File Offset: 0x000DC8A4
|
|
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
|
|
{
|
|
if (info == null)
|
|
{
|
|
throw new ArgumentNullException("info");
|
|
}
|
|
info.AddValue("TrackResurrection", this.TrackResurrection);
|
|
try
|
|
{
|
|
info.AddValue("TrackedObject", this.Target);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
info.AddValue("TrackedObject", null);
|
|
}
|
|
}
|
|
|
|
// Token: 0x04001A40 RID: 6720
|
|
private bool isLongReference;
|
|
|
|
// Token: 0x04001A41 RID: 6721
|
|
private GCHandle gcHandle;
|
|
}
|
|
}
|