Qbservable.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_EXPRESSIONS
  3. #pragma warning disable 1591
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. using System.Reactive.Concurrency;
  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. public static partial class Qbservable
  15. {
  16. /// <summary>
  17. /// Returns the input typed as an IObservable&lt;TSource&gt;.
  18. /// This operator is used to separate the part of the query that's captured as an expression tree from the part that's executed locally.
  19. /// </summary>
  20. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  21. /// <param name="source">An IQbservable&lt;TSource&gt; sequence to convert to an IObservable&lt;TSource&gt; sequence.</param>
  22. /// <returns>The original source object, but typed as an IObservable&lt;TSource&gt;.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  24. public static IObservable<TSource> AsObservable<TSource>(this IQbservable<TSource> source)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException("source");
  28. return source;
  29. }
  30. /// <summary>
  31. /// Converts an enumerable sequence to an observable sequence.
  32. /// </summary>
  33. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  34. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  35. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  36. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  37. /// <remarks>This operator requires the source's <see cref="IQueryProvider"/> object (see <see cref="IQueryable.Provider"/>) to implement <see cref="IQbservableProvider"/>.</remarks>
  38. public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource> source)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException("source");
  42. return ((IQbservableProvider)source.Provider).CreateQuery<TSource>(
  43. Expression.Call(
  44. null,
  45. #if CRIPPLED_REFLECTION
  46. InfoOf(() => Qbservable.ToQbservable<TSource>(default(IQueryable<TSource>))),
  47. #else
  48. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  49. #endif
  50. source.Expression
  51. )
  52. );
  53. }
  54. /// <summary>
  55. /// Converts an enumerable sequence to an observable sequence, using the specified scheduler to run the enumeration loop.
  56. /// </summary>
  57. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  58. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  59. /// <param name="scheduler">Scheduler to run the enumeration of the input sequence on.</param>
  60. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  61. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
  62. /// <remarks>This operator requires the source's <see cref="IQueryProvider"/> object (see <see cref="IQueryable.Provider"/>) to implement <see cref="IQbservableProvider"/>.</remarks>
  63. public static IQbservable<TSource> ToQbservable<TSource>(this IQueryable<TSource> source, IScheduler scheduler)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException("source");
  67. if (scheduler == null)
  68. throw new ArgumentNullException("scheduler");
  69. return ((IQbservableProvider)source.Provider).CreateQuery<TSource>(
  70. Expression.Call(
  71. null,
  72. #if CRIPPLED_REFLECTION
  73. InfoOf(() => Qbservable.ToQbservable<TSource>(default(IQueryable<TSource>))),
  74. #else
  75. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  76. #endif
  77. source.Expression,
  78. Expression.Constant(scheduler)
  79. )
  80. );
  81. }
  82. internal static Expression GetSourceExpression<TSource>(IObservable<TSource> source)
  83. {
  84. var q = source as IQbservable<TSource>;
  85. if (q != null)
  86. return q.Expression;
  87. return Expression.Constant(source, typeof(IObservable<TSource>));
  88. }
  89. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource> source)
  90. {
  91. var q = source as IQueryable<TSource>;
  92. if (q != null)
  93. return q.Expression;
  94. return Expression.Constant(source, typeof(IEnumerable<TSource>));
  95. }
  96. internal static Expression GetSourceExpression<TSource>(IObservable<TSource>[] sources)
  97. {
  98. return Expression.NewArrayInit(
  99. typeof(IObservable<TSource>),
  100. sources.Select(source => GetSourceExpression(source))
  101. );
  102. }
  103. internal static Expression GetSourceExpression<TSource>(IEnumerable<TSource>[] sources)
  104. {
  105. return Expression.NewArrayInit(
  106. typeof(IEnumerable<TSource>),
  107. sources.Select(source => GetSourceExpression(source))
  108. );
  109. }
  110. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  111. {
  112. return ((MethodCallExpression)f.Body).Method;
  113. }
  114. }
  115. }
  116. #pragma warning restore 1591
  117. #endif