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