Cast.cs 1.1 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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TResult> Cast<TResult>(this IAsyncEnumerable<object> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. // Check to see if it already is and short-circuit
  17. if (source is IAsyncEnumerable<TResult> typedSource)
  18. {
  19. return typedSource;
  20. }
  21. return source.Select(x => (TResult)x);
  22. }
  23. public static IAsyncEnumerable<TType> OfType<TType>(this IAsyncEnumerable<object> source)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. return source.Where(x => x is TType)
  28. .Cast<TType>();
  29. }
  30. }
  31. }