1
0

Cast.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;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. // NB: This is a non-standard LINQ operator, because we don't have a non-generic IAsyncEnumerable.
  12. // We're keeping it to enable `from T x in xs` binding in C#.
  13. public static IAsyncEnumerable<TResult> Cast<TResult>(this IAsyncEnumerable<object> source)
  14. {
  15. if (source == null)
  16. throw Error.ArgumentNull(nameof(source));
  17. if (source is IAsyncEnumerable<TResult> typedSource)
  18. {
  19. return typedSource;
  20. }
  21. return Create(Core);
  22. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  23. {
  24. await foreach (var obj in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  25. {
  26. yield return (TResult)obj;
  27. }
  28. }
  29. }
  30. }
  31. }