Observable.Remoting.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #if HAS_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. [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. {
  28. throw new ArgumentNullException(nameof(source));
  29. }
  30. return Remotable_(source);
  31. }
  32. /// <summary>
  33. /// Makes an observable sequence remotable, using a controllable lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  34. /// </summary>
  35. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  36. /// <param name="source">Source sequence.</param>
  37. /// <param name="lease">Lease object to control lifetime of the remotable sequence. Notice null is a supported value.</param>
  38. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  40. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  41. public static IObservable<TSource> Remotable<TSource>(this IObservable<TSource> source, ILease lease)
  42. {
  43. if (source == null)
  44. {
  45. throw new ArgumentNullException(nameof(source));
  46. }
  47. return Remotable_(source, lease);
  48. }
  49. /// <summary>
  50. /// Makes an observable sequence remotable, using an infinite lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  51. /// </summary>
  52. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  53. /// <param name="source">Source sequence.</param>
  54. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  55. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  56. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  57. public static IQbservable<TSource> Remotable<TSource>(this IQbservable<TSource> source)
  58. {
  59. if (source == null)
  60. {
  61. throw new ArgumentNullException(nameof(source));
  62. }
  63. return source.Provider.CreateQuery<TSource>(
  64. Expression.Call(
  65. null,
  66. #if CRIPPLED_REFLECTION
  67. InfoOf(() => RemotingObservable.Remotable<TSource>(default(IQbservable<TSource>))),
  68. #else
  69. #pragma warning disable IL2060 // ('System.Reflection.MethodInfo.MakeGenericMethod' can not be statically analyzed.) This gets the MethodInfo for the method running right now, so it can't have been trimmed
  70. ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  71. #pragma warning restore IL2060
  72. #endif
  73. source.Expression
  74. )
  75. );
  76. }
  77. /// <summary>
  78. /// Makes an observable sequence remotable, using a controllable lease for the <see cref="MarshalByRefObject"/> wrapping the source.
  79. /// </summary>
  80. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  81. /// <param name="source">Source sequence.</param>
  82. /// <param name="lease">Lease object to control lifetime of the remotable sequence. Notice null is a supported value.</param>
  83. /// <returns>The observable sequence that supports remote subscriptions.</returns>
  84. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  85. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Remotable", Justification = "In honor of the .NET Remoting heroes.")]
  86. public static IQbservable<TSource> Remotable<TSource>(this IQbservable<TSource> source, ILease lease)
  87. {
  88. if (source == null)
  89. {
  90. throw new ArgumentNullException(nameof(source));
  91. }
  92. return source.Provider.CreateQuery<TSource>(
  93. Expression.Call(
  94. null,
  95. #if CRIPPLED_REFLECTION
  96. InfoOf(() => RemotingObservable.Remotable<TSource>(default(IQbservable<TSource>), default(ILease))),
  97. #else
  98. #pragma warning disable IL2060 // ('System.Reflection.MethodInfo.MakeGenericMethod' can not be statically analyzed.) This gets the MethodInfo for the method running right now, so it can't have been trimmed
  99. ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource)),
  100. #pragma warning restore IL2060
  101. #endif
  102. source.Expression,
  103. Expression.Constant(lease, typeof(ILease))
  104. )
  105. );
  106. }
  107. #if CRIPPLED_REFLECTION
  108. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  109. {
  110. return ((MethodCallExpression)f.Body).Method;
  111. }
  112. #endif
  113. #endregion
  114. }
  115. }
  116. #endif