Cast.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. var typedSource = source as IAsyncEnumerable<TResult>;
  18. if (typedSource != null)
  19. {
  20. return typedSource;
  21. }
  22. return source.Select(x => (TResult)x);
  23. }
  24. public static IAsyncEnumerable<TType> OfType<TType>(this IAsyncEnumerable<object> source)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. return source.Where(x => x is TType)
  29. .Cast<TType>();
  30. }
  31. }
  32. }