MultipleAssignmentDisposable.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #nullable disable
  5. namespace System.Reactive.Disposables
  6. {
  7. /// <summary>
  8. /// Represents a disposable resource whose underlying disposable resource can be swapped for another disposable resource.
  9. /// </summary>
  10. public sealed class MultipleAssignmentDisposable : ICancelable
  11. {
  12. private IDisposable _current;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="MultipleAssignmentDisposable"/> class with no current underlying disposable.
  15. /// </summary>
  16. public MultipleAssignmentDisposable()
  17. {
  18. }
  19. /// <summary>
  20. /// Gets a value that indicates whether the object is disposed.
  21. /// </summary>
  22. public bool IsDisposed => Disposables.Disposable.GetIsDisposed(ref _current);
  23. /// <summary>
  24. /// Gets or sets the underlying disposable. After disposal, the result of getting this property is undefined.
  25. /// </summary>
  26. /// <remarks>If the <see cref="MultipleAssignmentDisposable"/> has already been disposed, assignment to this property causes immediate disposal of the given disposable object.</remarks>
  27. public IDisposable Disposable
  28. {
  29. get => Disposables.Disposable.GetValueOrDefault(ref _current);
  30. set => Disposables.Disposable.TrySetMultiple(ref _current, value);
  31. }
  32. /// <summary>
  33. /// Disposes the underlying disposable as well as all future replacements.
  34. /// </summary>
  35. public void Dispose()
  36. {
  37. Disposables.Disposable.TryDispose(ref _current);
  38. }
  39. }
  40. }