Do.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Lazily invokes an action for each value in the sequence.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="onNext">Action to invoke for each element.</param>
  15. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  16. public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. if (onNext == null)
  21. throw new ArgumentNullException(nameof(onNext));
  22. return DoCore(source, onNext, _ => { }, () => { });
  23. }
  24. /// <summary>
  25. /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <param name="source">Source sequence.</param>
  29. /// <param name="onNext">Action to invoke for each element.</param>
  30. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  31. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  32. public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (onNext == null)
  37. throw new ArgumentNullException(nameof(onNext));
  38. if (onCompleted == null)
  39. throw new ArgumentNullException(nameof(onCompleted));
  40. return DoCore(source, onNext, _ => { }, onCompleted);
  41. }
  42. /// <summary>
  43. /// Lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination.
  44. /// </summary>
  45. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  46. /// <param name="source">Source sequence.</param>
  47. /// <param name="onNext">Action to invoke for each element.</param>
  48. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  49. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  50. public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  51. {
  52. if (source == null)
  53. throw new ArgumentNullException(nameof(source));
  54. if (onNext == null)
  55. throw new ArgumentNullException(nameof(onNext));
  56. if (onError == null)
  57. throw new ArgumentNullException(nameof(onError));
  58. return DoCore(source, onNext, onError, () => { });
  59. }
  60. /// <summary>
  61. /// Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional
  62. /// termination.
  63. /// </summary>
  64. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  65. /// <param name="source">Source sequence.</param>
  66. /// <param name="onNext">Action to invoke for each element.</param>
  67. /// <param name="onError">Action to invoke on exceptional termination of the sequence.</param>
  68. /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
  69. /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
  70. public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  71. {
  72. if (source == null)
  73. throw new ArgumentNullException(nameof(source));
  74. if (onNext == null)
  75. throw new ArgumentNullException(nameof(onNext));
  76. if (onError == null)
  77. throw new ArgumentNullException(nameof(onError));
  78. if (onCompleted == null)
  79. throw new ArgumentNullException(nameof(onCompleted));
  80. return DoCore(source, onNext, onError, onCompleted);
  81. }
  82. /// <summary>
  83. /// Lazily invokes observer methods for each value in the sequence, and upon successful or exceptional termination.
  84. /// </summary>
  85. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  86. /// <param name="source">Source sequence.</param>
  87. /// <param name="observer">Observer to invoke notification calls on.</param>
  88. /// <returns>Sequence exhibiting the side-effects of observer method invocation upon enumeration.</returns>
  89. public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)
  90. {
  91. if (source == null)
  92. throw new ArgumentNullException(nameof(source));
  93. if (observer == null)
  94. throw new ArgumentNullException(nameof(observer));
  95. return DoCore(source, observer.OnNext, observer.OnError, observer.OnCompleted);
  96. }
  97. private static IEnumerable<TSource> DoCore<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  98. {
  99. using var e = source.GetEnumerator();
  100. while (true)
  101. {
  102. TSource current;
  103. try
  104. {
  105. if (!e.MoveNext())
  106. break;
  107. current = e.Current;
  108. }
  109. catch (Exception ex)
  110. {
  111. onError(ex);
  112. throw;
  113. }
  114. onNext(current);
  115. yield return current;
  116. }
  117. onCompleted();
  118. }
  119. }
  120. }