WeakReference.T.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.Serialization;
  5. using System.Runtime.CompilerServices;
  6. using System.Diagnostics.CodeAnalysis;
  7. namespace System
  8. {
  9. [Serializable]
  10. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  11. // This class is sealed to mitigate security issues caused by Object::MemberwiseClone.
  12. public sealed partial class WeakReference<T> : ISerializable
  13. where T : class?
  14. {
  15. // If you fix bugs here, please fix them in WeakReference at the same time.
  16. // Creates a new WeakReference that keeps track of target.
  17. // Assumes a Short Weak Reference (ie TrackResurrection is false.)
  18. //
  19. public WeakReference(T target)
  20. : this(target, false)
  21. {
  22. }
  23. //Creates a new WeakReference that keeps track of target.
  24. //
  25. public WeakReference(T target, bool trackResurrection)
  26. {
  27. Create(target, trackResurrection);
  28. }
  29. internal WeakReference(SerializationInfo info, StreamingContext context)
  30. {
  31. if (info == null)
  32. {
  33. throw new ArgumentNullException(nameof(info));
  34. }
  35. T target = (T)info.GetValue("TrackedObject", typeof(T))!; // Do not rename (binary serialization)
  36. bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)
  37. Create(target, trackResurrection);
  38. }
  39. //
  40. // We are exposing TryGetTarget instead of a simple getter to avoid a common problem where people write incorrect code like:
  41. //
  42. // WeakReference ref = ...;
  43. // if (ref.Target != null)
  44. // DoSomething(ref.Target)
  45. //
  46. [MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
  47. public bool TryGetTarget([MaybeNullWhen(false), NotNullWhen(true)] out T target)
  48. {
  49. // Call the worker method that has more performant but less user friendly signature.
  50. T o = this.Target;
  51. target = o;
  52. return o != null;
  53. }
  54. public void GetObjectData(SerializationInfo info, StreamingContext context)
  55. {
  56. if (info == null)
  57. {
  58. throw new ArgumentNullException(nameof(info));
  59. }
  60. info.AddValue("TrackedObject", this.Target, typeof(T)); // Do not rename (binary serialization)
  61. info.AddValue("TrackResurrection", IsTrackResurrection()); // Do not rename (binary serialization)
  62. }
  63. }
  64. }