AsyncQueryable.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace System.Linq
  6. {
  7. /// <summary>
  8. /// Provides a set of extension methods for asynchronous enumerable sequences represented using expression trees.
  9. /// </summary>
  10. public static partial class AsyncQueryable
  11. {
  12. #if !CRIPPLED_REFLECTION
  13. /// <summary>
  14. /// Converts the specified asynchronous enumerable sequence to an expression representation.
  15. /// </summary>
  16. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  17. /// <param name="source">The asynchronous enumerable sequence to represent using an expression tree.</param>
  18. /// <returns>An asynchronous enumerable sequence using an expression tree to represent the specified asynchronous enumerable sequence.</returns>
  19. public static IAsyncQueryable<TElement> AsAsyncQueryable<TElement>(this IAsyncEnumerable<TElement> source)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException("source");
  23. var queryable = source as IAsyncQueryable<TElement>;
  24. if (queryable != null)
  25. {
  26. return queryable;
  27. }
  28. return new AsyncEnumerableQuery<TElement>(source);
  29. }
  30. #endif
  31. private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)
  32. {
  33. var queryable = source as IAsyncQueryable<TSource>;
  34. if (queryable != null)
  35. {
  36. return queryable.Expression;
  37. }
  38. return Expression.Constant(source, typeof(IAsyncEnumerable<TSource>));
  39. }
  40. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  41. {
  42. return ((MethodCallExpression)f.Body).Method;
  43. }
  44. }
  45. }