1
0

SerialDisposable.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. namespace System.Reactive.Disposables
  5. {
  6. /// <summary>
  7. /// Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
  8. /// </summary>
  9. public sealed class SerialDisposable : ICancelable
  10. {
  11. private SerialDisposableValue _current;
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.SerialDisposable"/> class.
  14. /// </summary>
  15. public SerialDisposable()
  16. {
  17. }
  18. /// <summary>
  19. /// Gets a value that indicates whether the object is disposed.
  20. /// </summary>
  21. public bool IsDisposed => _current.IsDisposed;
  22. /// <summary>
  23. /// Gets or sets the underlying disposable.
  24. /// </summary>
  25. /// <remarks>If the SerialDisposable has already been disposed, assignment to this property causes immediate disposal of the given disposable object. Assigning this property disposes the previous disposable object.</remarks>
  26. public IDisposable? Disposable
  27. {
  28. get => _current.Disposable;
  29. set => _current.Disposable = value;
  30. }
  31. /// <summary>
  32. /// Disposes the underlying disposable as well as all future replacements.
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. _current.Dispose();
  37. }
  38. }
  39. }