Observable.Remoting.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_REMOTING
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Runtime.Remoting.Lifetime;
  8. namespace System.Reactive.Linq
  9. {
  10. /// <summary>
  11. /// Provides a set of static methods for exposing observable sequences through .NET Remoting.
  12. /// </summary>
  13. public static partial class RemotingObservable
  14. {
  15. #region Remotable
  16. /// <summary>
  17. /// Makes an observable sequence remotable, using an infinite lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  18. /// </summary>
  19. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  20. /// <param name="source">Source sequence.</param>
  21. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  22. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  23. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  24. public static IObservable<TSource> Remotable<TSource>(this IObservable<TSource> source)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. return Remotable_<TSource>(source);
  29. }
  30. /// <summary>
  31. /// Makes an observable sequence remotable, using a controllable lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  32. /// </summary>
  33. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  34. /// <param name="source">Source sequence.</param>
  35. /// <param name="lease">Lease object to control lifetime of the remotable sequence. Notice null is a supported value.</param>
  36. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  37. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  38. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  39. public static IObservable<TSource> Remotable<TSource>(this IObservable<TSource> source, ILease lease)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. return Remotable_<TSource>(source, lease);
  44. }
  45. /// <summary>
  46. /// Makes an observable sequence remotable, using an infinite lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  47. /// </summary>
  48. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  49. /// <param name="source">Source sequence.</param>
  50. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  51. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  52. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  53. public static IQbservable<TSource> Remotable<TSource>(this IQbservable<TSource> source)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. return source.Provider.CreateQuery<TSource>(
  58. Expression.Call(
  59. null,
  60. #if CRIPPLED_REFLECTION
  61. InfoOf(() => RemotingObservable.Remotable<TSource>(default(IQbservable<TSource>))),
  62. #else
  63. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  64. #endif
  65. source.Expression
  66. )
  67. );
  68. }
  69. /// <summary>
  70. /// Makes an observable sequence remotable, using a controllable lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  71. /// </summary>
  72. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  73. /// <param name="source">Source sequence.</param>
  74. /// <param name="lease">Lease object to control lifetime of the remotable sequence. Notice null is a supported value.</param>
  75. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  76. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  77. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  78. public static IQbservable<TSource> Remotable<TSource>(this IQbservable<TSource> source, ILease lease)
  79. {
  80. if (source == null)
  81. throw new ArgumentNullException(nameof(source));
  82. return source.Provider.CreateQuery<TSource>(
  83. Expression.Call(
  84. null,
  85. #if CRIPPLED_REFLECTION
  86. InfoOf(() => RemotingObservable.Remotable<TSource>(default(IQbservable<TSource>), default(ILease))),
  87. #else
  88. ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  89. #endif
  90. source.Expression,
  91. Expression.Constant(lease, typeof(ILease))
  92. )
  93. );
  94. }
  95. #if CRIPPLED_REFLECTION
  96. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  97. {
  98. return ((MethodCallExpression)f.Body).Method;
  99. }
  100. #endif
  101. #endregion
  102. }
  103. }
  104. #endif