Mirror of Svelto.ECS because we're a fan of it
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.

WeakReference.cs 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. /// <span class="code-SummaryComment"><summary></span>
  3. /// Represents a weak reference, which references an object while still allowing
  4. /// that object to be reclaimed by garbage collection.
  5. /// <span class="code-SummaryComment"></summary></span>
  6. /// <span class="code-SummaryComment"><typeparam name="T">The type of the object that is referenced.</typeparam></span>
  7. namespace Svelto.DataStructures
  8. {
  9. #if !NETFX_CORE
  10. public class WeakReference<T>
  11. : WeakReference where T : class
  12. {
  13. public bool IsValid { get { return Target != null && IsAlive == true; } }
  14. /// <span class="code-SummaryComment"><summary></span>
  15. /// Gets or sets the object (the target) referenced by the
  16. /// current WeakReference{T} object.
  17. /// <span class="code-SummaryComment"></summary></span>
  18. public new T Target
  19. {
  20. get
  21. {
  22. return (T)base.Target;
  23. }
  24. set
  25. {
  26. base.Target = value;
  27. }
  28. }
  29. /// <span class="code-SummaryComment"><summary></span>
  30. /// Initializes a new instance of the WeakReference{T} class, referencing
  31. /// the specified object.
  32. /// <span class="code-SummaryComment"></summary></span>
  33. /// <span class="code-SummaryComment"><param name="target">The object to reference.</param></span>
  34. public WeakReference(T target)
  35. : base(target)
  36. { }
  37. /// <span class="code-SummaryComment"><summary></span>
  38. /// Initializes a new instance of the WeakReference{T} class, referencing
  39. /// the specified object and using the specified resurrection tracking.
  40. /// <span class="code-SummaryComment"></summary></span>
  41. /// <span class="code-SummaryComment"><param name="target">An object to track.</param></span>
  42. /// <span class="code-SummaryComment"><param name="trackResurrection">Indicates when to stop tracking the object. </span>
  43. /// If true, the object is tracked
  44. /// after finalization; if false, the object is only tracked
  45. /// until finalization.<span class="code-SummaryComment"></param></span>
  46. public WeakReference(T target, bool trackResurrection)
  47. : base(target, trackResurrection)
  48. { }
  49. }
  50. #else
  51. public class WeakReference<T> : System.WeakReference where T : class
  52. {
  53. public bool IsValid { get { return Target != null && IsAlive == true; } }
  54. public new T Target
  55. {
  56. get
  57. {
  58. return (T)base.Target;
  59. }
  60. set
  61. {
  62. base.Target = value;
  63. }
  64. }
  65. public WeakReference(T target)
  66. : base(target)
  67. { }
  68. public WeakReference(T target, bool trackResurrection)
  69. : base(target, trackResurrection)
  70. { }
  71. }
  72. #endif
  73. }