QbservableEx.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #if !NO_EXPRESSIONS
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Reflection;
  9. namespace System.Reactive.Linq
  10. {
  11. /// <summary>
  12. /// Provides a set of static methods for writing queries over observable sequences, allowing translation to a target query language.
  13. /// </summary>
  14. [LocalQueryMethodImplementationType(typeof(ObservableEx))]
  15. public static partial class QbservableEx
  16. {
  17. internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
  18. {
  19. var q = source as IQbservable<TSource>;
  20. if (q != null)
  21. return q.Expression;
  22. return Expression.Constant(source, typeof(IObservable<TSource>));
  23. }
  24. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
  25. {
  26. var q = source as IQueryable<TSource>;
  27. if (q != null)
  28. return q.Expression;
  29. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  30. }
  31. internal static Expression GetSourceExpression<TSource>(IObservable<TSource>[] sources)
  32. {
  33. return Expression.NewArrayInit(
  34. typeof(IObservable<TSource>),
  35. sources.Select(source => GetSourceExpression(source))
  36. );
  37. }
  38. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
  39. {
  40. return Expression.NewArrayInit(
  41. typeof(IEnumerable<TSource>),
  42. sources.Select(source => GetSourceExpression(source))
  43. );
  44. }
  45. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  46. {
  47. return ((MethodCallExpression)f.Body).Method;
  48. }
  49. }
  50. }
  51. #endif