ToEnumerable.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace System.Linq
  7. {
  8. #if REFERENCE_ASSEMBLY
  9. public static partial class AsyncEnumerableDeprecated
  10. #else
  11. public static partial class AsyncEnumerable
  12. #endif
  13. {
  14. // NOTE: This type of blocking is an anti-pattern. We should never have offered it.
  15. // It is being left here for binary compatibility for those who were using it,
  16. // and the publicly visible version is marked as Obsolete so we can explain
  17. // why it should not be used.
  18. /// <summary>
  19. /// Converts an async-enumerable sequence to an enumerable sequence.
  20. /// </summary>
  21. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  22. /// <param name="source">An async-enumerable sequence to convert to an enumerable sequence.</param>
  23. /// <returns>The enumerable sequence containing the elements in the async-enumerable sequence.</returns>
  24. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  25. [Obsolete("IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and it does not implement this method because 'sync over async' of this kind is an anti-pattern. Please use a different strategy.")]
  26. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  27. {
  28. if (source == null)
  29. throw Error.ArgumentNull(nameof(source));
  30. return Core(source);
  31. static IEnumerable<TSource> Core(IAsyncEnumerable<TSource> source)
  32. {
  33. var e = source.GetAsyncEnumerator(default);
  34. try
  35. {
  36. while (true)
  37. {
  38. if (!Wait(e.MoveNextAsync()))
  39. break;
  40. yield return e.Current;
  41. }
  42. }
  43. finally
  44. {
  45. Wait(e.DisposeAsync());
  46. }
  47. }
  48. }
  49. // NB: ValueTask and ValueTask<T> do not have to support blocking on a call to GetResult when backed by
  50. // an IValueTaskSource or IValueTaskSource<T> implementation. Convert to a Task or Task<T> to do so
  51. // in case the task hasn't completed yet.
  52. private static void Wait(ValueTask task)
  53. {
  54. var awaiter = task.GetAwaiter();
  55. if (!awaiter.IsCompleted)
  56. {
  57. task.AsTask().GetAwaiter().GetResult();
  58. return;
  59. }
  60. awaiter.GetResult();
  61. }
  62. private static T Wait<T>(ValueTask<T> task)
  63. {
  64. var awaiter = task.GetAwaiter();
  65. if (!awaiter.IsCompleted)
  66. {
  67. return task.AsTask().GetAwaiter().GetResult();
  68. }
  69. return awaiter.GetResult();
  70. }
  71. }
  72. }