12345678910111213141516171819202122232425262728293031323334353637 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- // NB: This is a non-standard LINQ operator, because we don't have a non-generic IAsyncEnumerable.
- // We're keeping it to enable `from T x in xs` binding in C#.
- public static IAsyncEnumerable<TResult> Cast<TResult>(this IAsyncEnumerable<object> source)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (source is IAsyncEnumerable<TResult> typedSource)
- {
- return typedSource;
- }
- return Create(Core);
- async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
- {
- await foreach (var obj in source.WithCancellation(cancellationToken).ConfigureAwait(false))
- {
- yield return (TResult)obj;
- }
- }
- }
- }
- }
|