QbservableEx.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. namespace System.Reactive.Linq
  9. {
  10. /// <summary>
  11. /// Provides a set of static methods for writing queries over observable sequences, allowing translation to a target query language.
  12. /// </summary>
  13. [LocalQueryMethodImplementationType(typeof(ObservableEx))]
  14. public static partial class QbservableEx
  15. {
  16. internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
  17. {
  18. if (source is IQbservable<TSource> q)
  19. {
  20. return q.Expression;
  21. }
  22. return Expression.Constant(source, typeof(IObservable<TSource>));
  23. }
  24. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
  25. {
  26. if (source is IQueryable<TSource> q)
  27. {
  28. return q.Expression;
  29. }
  30. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  31. }
  32. internal static Expression GetSourceExpression<TSource>(IObservable<TSource>[] sources)
  33. {
  34. return Expression.NewArrayInit(
  35. typeof(IObservable<TSource>),
  36. sources.Select(source => GetSourceExpression(source))
  37. );
  38. }
  39. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
  40. {
  41. return Expression.NewArrayInit(
  42. typeof(IEnumerable<TSource>),
  43. sources.Select(source => GetSourceExpression(source))
  44. );
  45. }
  46. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  47. {
  48. return ((MethodCallExpression)f.Body).Method;
  49. }
  50. }
  51. }