Throw.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerableEx
  9. {
  10. public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  11. {
  12. if (exception == null)
  13. throw Error.ArgumentNull(nameof(exception));
  14. #if NO_TASK_FROMEXCEPTION
  15. var tcs = new TaskCompletionSource<bool>();
  16. tcs.TrySetException(exception);
  17. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  18. #else
  19. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  20. #endif
  21. //
  22. // REVIEW: Honor cancellation using conditional expression in MoveNextAsync?
  23. //
  24. return AsyncEnumerable.CreateEnumerable(
  25. _ => AsyncEnumerator.Create<TValue>(
  26. () => moveNextThrows,
  27. current: null,
  28. dispose: null)
  29. );
  30. }
  31. }
  32. }