ConfiguredAsyncDisposable.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. #if !HAS_ASYNCDISPOSABLE
  2. // Licensed to the .NET Foundation under one or more agreements.
  3. // The .NET Foundation licenses this file to you under the MIT license.
  4. // See the LICENSE file in the project root for more information.
  5. using System.Runtime.InteropServices;
  6. namespace System.Runtime.CompilerServices
  7. {
  8. /// <summary>Provides a type that can be used to configure how awaits on an <see cref="IAsyncDisposable"/> are performed.</summary>
  9. [StructLayout(LayoutKind.Auto)]
  10. public readonly struct ConfiguredAsyncDisposable
  11. {
  12. private readonly IAsyncDisposable _source;
  13. private readonly bool _continueOnCapturedContext;
  14. internal ConfiguredAsyncDisposable(IAsyncDisposable source, bool continueOnCapturedContext)
  15. {
  16. _source = source;
  17. _continueOnCapturedContext = continueOnCapturedContext;
  18. }
  19. public ConfiguredValueTaskAwaitable DisposeAsync() =>
  20. // as with other "configured" awaitable-related type in CompilerServices, we don't null check to defend against
  21. // misuse like `default(ConfiguredAsyncDisposable).DisposeAsync()`, which will null ref by design.
  22. _source.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  23. }
  24. }
  25. #else
  26. using System.Runtime.CompilerServices;
  27. [assembly: TypeForwardedTo(typeof(ConfiguredAsyncDisposable))]
  28. #endif