You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Serialization;
  6. using System.Security;
  7. using System.Text;
  8. namespace System
  9. {
  10. // this is literally jus8t the decompilation of Mono's .NET 4 implementation
  11. /// <summary>Represents a typed weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.</summary>
  12. /// <typeparam name="T">The type of the object referenced.</typeparam>
  13. // Token: 0x02000248 RID: 584
  14. [Serializable]
  15. public sealed class WeakReference<T> : ISerializable where T : class
  16. {
  17. /// <summary>Initializes a new instance of the <see cref="T:System.WeakReference`1" /> class that references the specified object.</summary>
  18. /// <param name="target">The object to reference, or <see langword="null" />.</param>
  19. // Token: 0x06001B8A RID: 7050 RVA: 0x00068700 File Offset: 0x00066900
  20. public WeakReference(T target) : this(target, false)
  21. {
  22. }
  23. /// <summary>Initializes a new instance of the <see cref="T:System.WeakReference`1" /> class that references the specified object and uses the specified resurrection tracking.</summary>
  24. /// <param name="target">The object to reference, or <see langword="null" />.</param>
  25. /// <param name="trackResurrection">
  26. /// <see langword="true" /> to track the object after finalization; <see langword="false" /> to track the object only until finalization.</param>
  27. // Token: 0x06001B8B RID: 7051 RVA: 0x0006870C File Offset: 0x0006690C
  28. public WeakReference(T target, bool trackResurrection)
  29. {
  30. this.trackResurrection = trackResurrection;
  31. GCHandleType type = trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak;
  32. handle = GCHandle.Alloc(target, type);
  33. }
  34. // Token: 0x06001B8C RID: 7052 RVA: 0x00068740 File Offset: 0x00066940
  35. private WeakReference(SerializationInfo info, StreamingContext context)
  36. {
  37. if (info == null)
  38. {
  39. throw new ArgumentNullException(nameof(info));
  40. }
  41. trackResurrection = info.GetBoolean("TrackResurrection");
  42. object value = info.GetValue("TrackedObject", typeof(T));
  43. GCHandleType type = trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak;
  44. handle = GCHandle.Alloc(value, type);
  45. }
  46. /// <summary>Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object with all the data necessary to serialize the current <see cref="T:System.WeakReference`1" /> object.</summary>
  47. /// <param name="info">An object that holds all the data necessary to serialize or deserialize the current <see cref="T:System.WeakReference`1" /> object.</param>
  48. /// <param name="context">The location where serialized data is stored and retrieved.</param>
  49. /// <exception cref="T:System.ArgumentNullException">
  50. /// <paramref name="info" /> is <see langword="null" />. </exception>
  51. // Token: 0x06001B8D RID: 7053 RVA: 0x000687A4 File Offset: 0x000669A4
  52. [SecurityCritical]
  53. public void GetObjectData(SerializationInfo info, StreamingContext context)
  54. {
  55. if (info == null)
  56. {
  57. throw new ArgumentNullException(nameof(info));
  58. }
  59. info.AddValue("TrackResurrection", trackResurrection);
  60. if (handle.IsAllocated)
  61. {
  62. info.AddValue("TrackedObject", handle.Target);
  63. return;
  64. }
  65. info.AddValue("TrackedObject", null);
  66. }
  67. /// <summary>Sets the target object that is referenced by this <see cref="T:System.WeakReference`1" /> object.</summary>
  68. /// <param name="target">The new target object.</param>
  69. // Token: 0x06001B8E RID: 7054 RVA: 0x00068800 File Offset: 0x00066A00
  70. public void SetTarget(T target)
  71. {
  72. handle.Target = target;
  73. }
  74. /// <summary>Tries to retrieve the target object that is referenced by the current <see cref="T:System.WeakReference`1" /> object.</summary>
  75. /// <param name="target">When this method returns, contains the target object, if it is available. This parameter is treated as uninitialized.</param>
  76. /// <returns>
  77. /// <see langword="true" /> if the target was retrieved; otherwise, <see langword="false" />.</returns>
  78. // Token: 0x06001B8F RID: 7055 RVA: 0x00068813 File Offset: 0x00066A13
  79. public bool TryGetTarget(out T target)
  80. {
  81. if (!handle.IsAllocated)
  82. {
  83. target = default;
  84. return false;
  85. }
  86. target = (T)handle.Target;
  87. return target != null;
  88. }
  89. /// <summary>Discards the reference to the target that is represented by the current <see cref="T:System.WeakReference`1" /> object.</summary>
  90. // Token: 0x06001B90 RID: 7056 RVA: 0x00068850 File Offset: 0x00066A50
  91. ~WeakReference()
  92. {
  93. handle.Free();
  94. }
  95. // Token: 0x04000F59 RID: 3929
  96. private GCHandle handle;
  97. // Token: 0x04000F5A RID: 3930
  98. private bool trackResurrection;
  99. }
  100. }