Throw.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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 new ArgumentNullException(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. return AsyncEnumerable.CreateEnumerable(
  22. () => AsyncEnumerable.CreateEnumerator<TValue>(
  23. () => moveNextThrows,
  24. current: null,
  25. dispose: null)
  26. );
  27. }
  28. }
  29. }