WeakReference.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. namespace System
  6. {
  7. [Serializable]
  8. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  9. public partial class WeakReference : ISerializable
  10. {
  11. // If you fix bugs here, please fix them in WeakReference<T> at the same time.
  12. // Creates a new WeakReference that keeps track of target.
  13. // Assumes a Short Weak Reference (ie TrackResurrection is false.)
  14. //
  15. public WeakReference(object? target)
  16. : this(target, false)
  17. {
  18. }
  19. public WeakReference(object? target, bool trackResurrection)
  20. {
  21. Create(target, trackResurrection);
  22. }
  23. protected WeakReference(SerializationInfo info, StreamingContext context)
  24. {
  25. if (info == null)
  26. {
  27. throw new ArgumentNullException(nameof(info));
  28. }
  29. object? target = info.GetValue("TrackedObject", typeof(object)); // Do not rename (binary serialization)
  30. bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)
  31. Create(target, trackResurrection);
  32. }
  33. public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  34. {
  35. if (info == null)
  36. {
  37. throw new ArgumentNullException(nameof(info));
  38. }
  39. info.AddValue("TrackedObject", Target, typeof(object)); // Do not rename (binary serialization)
  40. info.AddValue("TrackResurrection", IsTrackResurrection()); // Do not rename (binary serialization)
  41. }
  42. }
  43. }