Observable.Events.cs 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. using System.Reactive.Concurrency;
  5. using System.Threading;
  6. namespace System.Reactive.Linq
  7. {
  8. public static partial class Observable
  9. {
  10. #region + FromEventPattern +
  11. #region Strongly typed
  12. #region Action<EventHandler>
  13. /// <summary>
  14. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler"/>, to an observable sequence.
  15. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  16. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  17. /// </summary>
  18. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  19. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  20. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  22. /// <remarks>
  23. /// <para>
  24. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  25. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  26. /// </para>
  27. /// <para>
  28. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  29. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  30. /// </para>
  31. /// <para>
  32. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  33. /// making the Subscribe or Dispose call, respectively.
  34. /// </para>
  35. /// <para>
  36. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  37. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  38. /// more concise and easier to understand.
  39. /// </para>
  40. /// </remarks>
  41. /// <seealso cref="Observable.ToEventPattern"/>
  42. #if !NO_EVENTARGS_CONSTRAINT
  43. public static IObservable<EventPattern<EventArgs>> FromEventPattern(Action<EventHandler> addHandler, Action<EventHandler> removeHandler)
  44. {
  45. if (addHandler == null)
  46. throw new ArgumentNullException("addHandler");
  47. if (removeHandler == null)
  48. throw new ArgumentNullException("removeHandler");
  49. return s_impl.FromEventPattern(addHandler, removeHandler);
  50. }
  51. #else
  52. public static IObservable<EventPattern<object>> FromEventPattern(Action<EventHandler> addHandler, Action<EventHandler> removeHandler)
  53. {
  54. if (addHandler == null)
  55. throw new ArgumentNullException(nameof(addHandler));
  56. if (removeHandler == null)
  57. throw new ArgumentNullException(nameof(removeHandler));
  58. return s_impl.FromEventPattern(addHandler, removeHandler);
  59. }
  60. #endif
  61. /// <summary>
  62. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler"/>, to an observable sequence.
  63. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  64. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  65. /// </summary>
  66. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  67. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  68. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  69. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  70. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  71. /// <remarks>
  72. /// <para>
  73. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  74. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  75. /// </para>
  76. /// <para>
  77. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  78. /// accessed from the same context, as required by some UI frameworks.
  79. /// </para>
  80. /// <para>
  81. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  82. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  83. /// parameter. For more information, see the remarks section on those overloads.
  84. /// </para>
  85. /// </remarks>
  86. /// <seealso cref="Observable.ToEventPattern"/>
  87. #if !NO_EVENTARGS_CONSTRAINT
  88. public static IObservable<EventPattern<EventArgs>> FromEventPattern(Action<EventHandler> addHandler, Action<EventHandler> removeHandler, IScheduler scheduler)
  89. {
  90. if (addHandler == null)
  91. throw new ArgumentNullException("addHandler");
  92. if (removeHandler == null)
  93. throw new ArgumentNullException("removeHandler");
  94. if (scheduler == null)
  95. throw new ArgumentNullException("scheduler");
  96. return s_impl.FromEventPattern(addHandler, removeHandler, scheduler);
  97. }
  98. #else
  99. public static IObservable<EventPattern<object>> FromEventPattern(Action<EventHandler> addHandler, Action<EventHandler> removeHandler, IScheduler scheduler)
  100. {
  101. if (addHandler == null)
  102. throw new ArgumentNullException(nameof(addHandler));
  103. if (removeHandler == null)
  104. throw new ArgumentNullException(nameof(removeHandler));
  105. if (scheduler == null)
  106. throw new ArgumentNullException(nameof(scheduler));
  107. return s_impl.FromEventPattern(addHandler, removeHandler, scheduler);
  108. }
  109. #endif
  110. #endregion
  111. #region Action<TDelegate>
  112. /// <summary>
  113. /// Converts a .NET event, conforming to the standard .NET event pattern based on a supplied event delegate type, to an observable sequence.
  114. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  115. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  116. /// </summary>
  117. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  118. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  119. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  120. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  121. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  122. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  123. /// <remarks>
  124. /// <para>
  125. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  126. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  127. /// </para>
  128. /// <para>
  129. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  130. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  131. /// </para>
  132. /// <para>
  133. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  134. /// making the Subscribe or Dispose call, respectively.
  135. /// </para>
  136. /// <para>
  137. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  138. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  139. /// more concise and easier to understand.
  140. /// </para>
  141. /// </remarks>
  142. /// <seealso cref="Observable.ToEventPattern"/>
  143. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  144. #if !NO_EVENTARGS_CONSTRAINT
  145. where TEventArgs : EventArgs
  146. #endif
  147. {
  148. if (addHandler == null)
  149. throw new ArgumentNullException(nameof(addHandler));
  150. if (removeHandler == null)
  151. throw new ArgumentNullException(nameof(removeHandler));
  152. return s_impl.FromEventPattern<TDelegate, TEventArgs>(addHandler, removeHandler);
  153. }
  154. /// <summary>
  155. /// Converts a .NET event, conforming to the standard .NET event pattern based on a supplied event delegate type, to an observable sequence.
  156. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  157. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  158. /// </summary>
  159. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  160. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  161. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  162. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  163. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  164. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  165. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  166. /// <remarks>
  167. /// <para>
  168. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  169. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  170. /// </para>
  171. /// <para>
  172. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  173. /// accessed from the same context, as required by some UI frameworks.
  174. /// </para>
  175. /// <para>
  176. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  177. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  178. /// parameter. For more information, see the remarks section on those overloads.
  179. /// </para>
  180. /// </remarks>
  181. /// <seealso cref="Observable.ToEventPattern"/>
  182. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler, IScheduler scheduler)
  183. #if !NO_EVENTARGS_CONSTRAINT
  184. where TEventArgs : EventArgs
  185. #endif
  186. {
  187. if (addHandler == null)
  188. throw new ArgumentNullException(nameof(addHandler));
  189. if (removeHandler == null)
  190. throw new ArgumentNullException(nameof(removeHandler));
  191. if (scheduler == null)
  192. throw new ArgumentNullException(nameof(scheduler));
  193. return s_impl.FromEventPattern<TDelegate, TEventArgs>(addHandler, removeHandler, scheduler);
  194. }
  195. /// <summary>
  196. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler&lt;TEventArgs&gt;"/>, to an observable sequence.
  197. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  198. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  199. /// </summary>
  200. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  201. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  202. /// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying .NET event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
  203. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  204. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  205. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  206. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  207. /// <remarks>
  208. /// <para>
  209. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  210. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  211. /// </para>
  212. /// <para>
  213. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  214. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  215. /// </para>
  216. /// <para>
  217. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  218. /// making the Subscribe or Dispose call, respectively.
  219. /// </para>
  220. /// <para>
  221. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  222. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  223. /// more concise and easier to understand.
  224. /// </para>
  225. /// </remarks>
  226. /// <seealso cref="Observable.ToEventPattern"/>
  227. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(Func<EventHandler<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  228. #if !NO_EVENTARGS_CONSTRAINT
  229. where TEventArgs : EventArgs
  230. #endif
  231. {
  232. if (conversion == null)
  233. throw new ArgumentNullException(nameof(conversion));
  234. if (addHandler == null)
  235. throw new ArgumentNullException(nameof(addHandler));
  236. if (removeHandler == null)
  237. throw new ArgumentNullException(nameof(removeHandler));
  238. return s_impl.FromEventPattern<TDelegate, TEventArgs>(conversion, addHandler, removeHandler);
  239. }
  240. /// <summary>
  241. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler&lt;TEventArgs&gt;"/>, to an observable sequence.
  242. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  243. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  244. /// </summary>
  245. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  246. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  247. /// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying .NET event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
  248. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  249. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  250. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  251. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  252. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  253. /// <remarks>
  254. /// <para>
  255. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  256. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  257. /// </para>
  258. /// <para>
  259. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  260. /// accessed from the same context, as required by some UI frameworks.
  261. /// </para>
  262. /// <para>
  263. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  264. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  265. /// parameter. For more information, see the remarks section on those overloads.
  266. /// </para>
  267. /// </remarks>
  268. /// <seealso cref="Observable.ToEventPattern"/>
  269. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(Func<EventHandler<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler, IScheduler scheduler)
  270. #if !NO_EVENTARGS_CONSTRAINT
  271. where TEventArgs : EventArgs
  272. #endif
  273. {
  274. if (conversion == null)
  275. throw new ArgumentNullException(nameof(conversion));
  276. if (addHandler == null)
  277. throw new ArgumentNullException(nameof(addHandler));
  278. if (removeHandler == null)
  279. throw new ArgumentNullException(nameof(removeHandler));
  280. if (scheduler == null)
  281. throw new ArgumentNullException(nameof(scheduler));
  282. return s_impl.FromEventPattern<TDelegate, TEventArgs>(conversion, addHandler, removeHandler, scheduler);
  283. }
  284. /// <summary>
  285. /// Converts a .NET event, conforming to the standard .NET event pattern based on a supplied event delegate type with a strongly typed sender parameter, to an observable sequence.
  286. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  287. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  288. /// </summary>
  289. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  290. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  291. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  292. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  293. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  294. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  295. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  296. /// <remarks>
  297. /// <para>
  298. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  299. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  300. /// </para>
  301. /// <para>
  302. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  303. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  304. /// </para>
  305. /// <para>
  306. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  307. /// making the Subscribe or Dispose call, respectively.
  308. /// </para>
  309. /// <para>
  310. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  311. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  312. /// more concise and easier to understand.
  313. /// </para>
  314. /// </remarks>
  315. /// <seealso cref="Observable.ToEventPattern"/>
  316. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TDelegate, TSender, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  317. #if !NO_EVENTARGS_CONSTRAINT
  318. where TEventArgs : EventArgs
  319. #endif
  320. {
  321. if (addHandler == null)
  322. throw new ArgumentNullException(nameof(addHandler));
  323. if (removeHandler == null)
  324. throw new ArgumentNullException(nameof(removeHandler));
  325. return s_impl.FromEventPattern<TDelegate, TSender, TEventArgs>(addHandler, removeHandler);
  326. }
  327. /// <summary>
  328. /// Converts a .NET event, conforming to the standard .NET event pattern based on a supplied event delegate type with a strongly typed sender parameter, to an observable sequence.
  329. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  330. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  331. /// </summary>
  332. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  333. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  334. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  335. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  336. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  337. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  338. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  339. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  340. /// <remarks>
  341. /// <para>
  342. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  343. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  344. /// </para>
  345. /// <para>
  346. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  347. /// accessed from the same context, as required by some UI frameworks.
  348. /// </para>
  349. /// <para>
  350. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  351. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  352. /// parameter. For more information, see the remarks section on those overloads.
  353. /// </para>
  354. /// </remarks>
  355. /// <seealso cref="Observable.ToEventPattern"/>
  356. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TDelegate, TSender, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler, IScheduler scheduler)
  357. #if !NO_EVENTARGS_CONSTRAINT
  358. where TEventArgs : EventArgs
  359. #endif
  360. {
  361. if (addHandler == null)
  362. throw new ArgumentNullException(nameof(addHandler));
  363. if (removeHandler == null)
  364. throw new ArgumentNullException(nameof(removeHandler));
  365. if (scheduler == null)
  366. throw new ArgumentNullException(nameof(scheduler));
  367. return s_impl.FromEventPattern<TDelegate, TSender, TEventArgs>(addHandler, removeHandler, scheduler);
  368. }
  369. #endregion
  370. #region Action<EventHandler<TEventArgs>>
  371. /// <summary>
  372. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler&lt;TEventArgs&gt;"/>, to an observable sequence.
  373. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  374. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  375. /// </summary>
  376. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  377. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  378. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  379. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  380. /// <remarks>
  381. /// <para>
  382. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  383. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  384. /// </para>
  385. /// <para>
  386. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  387. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  388. /// </para>
  389. /// <para>
  390. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  391. /// making the Subscribe or Dispose call, respectively.
  392. /// </para>
  393. /// <para>
  394. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  395. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  396. /// more concise and easier to understand.
  397. /// </para>
  398. /// </remarks>
  399. /// <seealso cref="Observable.ToEventPattern"/>
  400. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Action<EventHandler<TEventArgs>> addHandler, Action<EventHandler<TEventArgs>> removeHandler)
  401. #if !NO_EVENTARGS_CONSTRAINT
  402. where TEventArgs : EventArgs
  403. #endif
  404. {
  405. if (addHandler == null)
  406. throw new ArgumentNullException(nameof(addHandler));
  407. if (removeHandler == null)
  408. throw new ArgumentNullException(nameof(removeHandler));
  409. return s_impl.FromEventPattern<TEventArgs>(addHandler, removeHandler);
  410. }
  411. /// <summary>
  412. /// Converts a .NET event, conforming to the standard .NET event pattern based on <see cref="EventHandler&lt;TEventArgs&gt;"/>, to an observable sequence.
  413. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  414. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  415. /// </summary>
  416. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  417. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  418. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  419. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  420. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  421. /// <remarks>
  422. /// <para>
  423. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  424. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  425. /// </para>
  426. /// <para>
  427. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  428. /// accessed from the same context, as required by some UI frameworks.
  429. /// </para>
  430. /// <para>
  431. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  432. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  433. /// parameter. For more information, see the remarks section on those overloads.
  434. /// </para>
  435. /// </remarks>
  436. /// <seealso cref="Observable.ToEventPattern"/>
  437. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Action<EventHandler<TEventArgs>> addHandler, Action<EventHandler<TEventArgs>> removeHandler, IScheduler scheduler)
  438. #if !NO_EVENTARGS_CONSTRAINT
  439. where TEventArgs : EventArgs
  440. #endif
  441. {
  442. if (addHandler == null)
  443. throw new ArgumentNullException(nameof(addHandler));
  444. if (removeHandler == null)
  445. throw new ArgumentNullException(nameof(removeHandler));
  446. if (scheduler == null)
  447. throw new ArgumentNullException(nameof(scheduler));
  448. return s_impl.FromEventPattern<TEventArgs>(addHandler, removeHandler, scheduler);
  449. }
  450. #endregion
  451. #endregion
  452. #region Reflection
  453. #region Instance events
  454. /// <summary>
  455. /// Converts an instance .NET event, conforming to the standard .NET event pattern with an <see cref="EventArgs"/> parameter, to an observable sequence.
  456. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  457. /// Reflection is used to discover the event based on the target object type and the specified event name.
  458. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  459. /// </summary>
  460. /// <param name="target">Object instance that exposes the event to convert.</param>
  461. /// <param name="eventName">Name of the event to convert.</param>
  462. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  463. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> is null.</exception>
  464. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern.</exception>
  465. /// <remarks>
  466. /// <para>
  467. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  468. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  469. /// </para>
  470. /// <para>
  471. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  472. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  473. /// </para>
  474. /// <para>
  475. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  476. /// making the Subscribe or Dispose call, respectively.
  477. /// </para>
  478. /// <para>
  479. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  480. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  481. /// more concise and easier to understand.
  482. /// </para>
  483. /// </remarks>
  484. /// <seealso cref="Observable.ToEventPattern"/>
  485. #if !NO_EVENTARGS_CONSTRAINT
  486. public static IObservable<EventPattern<EventArgs>> FromEventPattern(object target, string eventName)
  487. {
  488. if (target == null)
  489. throw new ArgumentNullException("target");
  490. if (eventName == null)
  491. throw new ArgumentNullException("eventName");
  492. return s_impl.FromEventPattern(target, eventName);
  493. }
  494. #else
  495. public static IObservable<EventPattern<object>> FromEventPattern(object target, string eventName)
  496. {
  497. if (target == null)
  498. throw new ArgumentNullException(nameof(target));
  499. if (eventName == null)
  500. throw new ArgumentNullException(nameof(eventName));
  501. return s_impl.FromEventPattern(target, eventName);
  502. }
  503. #endif
  504. /// <summary>
  505. /// Converts an instance .NET event, conforming to the standard .NET event pattern with an <see cref="EventArgs"/> parameter, to an observable sequence.
  506. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  507. /// Reflection is used to discover the event based on the target object type and the specified event name.
  508. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  509. /// </summary>
  510. /// <param name="target">Object instance that exposes the event to convert.</param>
  511. /// <param name="eventName">Name of the event to convert.</param>
  512. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  513. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  514. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  515. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern.</exception>
  516. /// <remarks>
  517. /// <para>
  518. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  519. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  520. /// </para>
  521. /// <para>
  522. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  523. /// accessed from the same context, as required by some UI frameworks.
  524. /// </para>
  525. /// <para>
  526. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  527. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  528. /// parameter. For more information, see the remarks section on those overloads.
  529. /// </para>
  530. /// </remarks>
  531. /// <seealso cref="Observable.ToEventPattern"/>
  532. #if !NO_EVENTARGS_CONSTRAINT
  533. public static IObservable<EventPattern<EventArgs>> FromEventPattern(object target, string eventName, IScheduler scheduler)
  534. {
  535. if (target == null)
  536. throw new ArgumentNullException("target");
  537. if (eventName == null)
  538. throw new ArgumentNullException("eventName");
  539. if (scheduler == null)
  540. throw new ArgumentNullException("scheduler");
  541. return s_impl.FromEventPattern(target, eventName, scheduler);
  542. }
  543. #else
  544. public static IObservable<EventPattern<object>> FromEventPattern(object target, string eventName, IScheduler scheduler)
  545. {
  546. if (target == null)
  547. throw new ArgumentNullException(nameof(target));
  548. if (eventName == null)
  549. throw new ArgumentNullException(nameof(eventName));
  550. if (scheduler == null)
  551. throw new ArgumentNullException(nameof(scheduler));
  552. return s_impl.FromEventPattern(target, eventName, scheduler);
  553. }
  554. #endif
  555. /// <summary>
  556. /// Converts an instance .NET event, conforming to the standard .NET event pattern with strongly typed event arguments, to an observable sequence.
  557. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  558. /// Reflection is used to discover the event based on the target object type and the specified event name.
  559. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  560. /// </summary>
  561. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  562. /// <param name="target">Object instance that exposes the event to convert.</param>
  563. /// <param name="eventName">Name of the event to convert.</param>
  564. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  565. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> is null.</exception>
  566. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  567. /// <remarks>
  568. /// <para>
  569. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  570. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  571. /// </para>
  572. /// <para>
  573. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  574. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  575. /// </para>
  576. /// <para>
  577. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  578. /// making the Subscribe or Dispose call, respectively.
  579. /// </para>
  580. /// <para>
  581. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  582. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  583. /// more concise and easier to understand.
  584. /// </para>
  585. /// </remarks>
  586. /// <seealso cref="Observable.ToEventPattern"/>
  587. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(object target, string eventName)
  588. #if !NO_EVENTARGS_CONSTRAINT
  589. where TEventArgs : EventArgs
  590. #endif
  591. {
  592. if (target == null)
  593. throw new ArgumentNullException(nameof(target));
  594. if (eventName == null)
  595. throw new ArgumentNullException(nameof(eventName));
  596. return s_impl.FromEventPattern<TEventArgs>(target, eventName);
  597. }
  598. /// <summary>
  599. /// Converts an instance .NET event, conforming to the standard .NET event pattern with strongly typed event arguments, to an observable sequence.
  600. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  601. /// Reflection is used to discover the event based on the target object type and the specified event name.
  602. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  603. /// </summary>
  604. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  605. /// <param name="target">Object instance that exposes the event to convert.</param>
  606. /// <param name="eventName">Name of the event to convert.</param>
  607. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  608. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  609. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  610. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  611. /// <remarks>
  612. /// <para>
  613. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  614. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  615. /// </para>
  616. /// <para>
  617. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  618. /// accessed from the same context, as required by some UI frameworks.
  619. /// </para>
  620. /// <para>
  621. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  622. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  623. /// parameter. For more information, see the remarks section on those overloads.
  624. /// </para>
  625. /// </remarks>
  626. /// <seealso cref="Observable.ToEventPattern"/>
  627. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(object target, string eventName, IScheduler scheduler)
  628. #if !NO_EVENTARGS_CONSTRAINT
  629. where TEventArgs : EventArgs
  630. #endif
  631. {
  632. if (target == null)
  633. throw new ArgumentNullException(nameof(target));
  634. if (eventName == null)
  635. throw new ArgumentNullException(nameof(eventName));
  636. if (scheduler == null)
  637. throw new ArgumentNullException(nameof(scheduler));
  638. return s_impl.FromEventPattern<TEventArgs>(target, eventName, scheduler);
  639. }
  640. /// <summary>
  641. /// Converts an instance .NET event, conforming to the standard .NET event pattern with a strongly typed sender and strongly typed event arguments, to an observable sequence.
  642. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  643. /// Reflection is used to discover the event based on the target object type and the specified event name.
  644. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  645. /// </summary>
  646. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  647. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  648. /// <param name="target">Object instance that exposes the event to convert.</param>
  649. /// <param name="eventName">Name of the event to convert.</param>
  650. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  651. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> is null.</exception>
  652. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's first argument type is not assignable to TSender. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  653. /// <remarks>
  654. /// <para>
  655. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  656. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  657. /// </para>
  658. /// <para>
  659. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  660. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  661. /// </para>
  662. /// <para>
  663. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  664. /// making the Subscribe or Dispose call, respectively.
  665. /// </para>
  666. /// <para>
  667. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  668. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  669. /// more concise and easier to understand.
  670. /// </para>
  671. /// </remarks>
  672. /// <seealso cref="Observable.ToEventPattern"/>
  673. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TSender, TEventArgs>(object target, string eventName)
  674. #if !NO_EVENTARGS_CONSTRAINT
  675. where TEventArgs : EventArgs
  676. #endif
  677. {
  678. if (target == null)
  679. throw new ArgumentNullException(nameof(target));
  680. if (eventName == null)
  681. throw new ArgumentNullException(nameof(eventName));
  682. return s_impl.FromEventPattern<TSender, TEventArgs>(target, eventName);
  683. }
  684. /// <summary>
  685. /// Converts an instance .NET event, conforming to the standard .NET event pattern with a strongly typed sender and strongly typed event arguments, to an observable sequence.
  686. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  687. /// Reflection is used to discover the event based on the target object type and the specified event name.
  688. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  689. /// </summary>
  690. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  691. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  692. /// <param name="target">Object instance that exposes the event to convert.</param>
  693. /// <param name="eventName">Name of the event to convert.</param>
  694. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  695. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  696. /// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  697. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's first argument type is not assignable to TSender. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  698. /// <remarks>
  699. /// <para>
  700. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  701. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  702. /// </para>
  703. /// <para>
  704. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  705. /// accessed from the same context, as required by some UI frameworks.
  706. /// </para>
  707. /// <para>
  708. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  709. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  710. /// parameter. For more information, see the remarks section on those overloads.
  711. /// </para>
  712. /// </remarks>
  713. /// <seealso cref="Observable.ToEventPattern"/>
  714. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TSender, TEventArgs>(object target, string eventName, IScheduler scheduler)
  715. #if !NO_EVENTARGS_CONSTRAINT
  716. where TEventArgs : EventArgs
  717. #endif
  718. {
  719. if (target == null)
  720. throw new ArgumentNullException(nameof(target));
  721. if (eventName == null)
  722. throw new ArgumentNullException(nameof(eventName));
  723. if (scheduler == null)
  724. throw new ArgumentNullException(nameof(scheduler));
  725. return s_impl.FromEventPattern<TSender, TEventArgs>(target, eventName, scheduler);
  726. }
  727. #endregion
  728. #region Static events
  729. /// <summary>
  730. /// Converts a static .NET event, conforming to the standard .NET event pattern with an <see cref="EventArgs"/> parameter, to an observable sequence.
  731. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  732. /// Reflection is used to discover the event based on the specified type and the specified event name.
  733. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  734. /// </summary>
  735. /// <param name="type">Type that exposes the static event to convert.</param>
  736. /// <param name="eventName">Name of the event to convert.</param>
  737. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  738. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> is null.</exception>
  739. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern.</exception>
  740. /// <remarks>
  741. /// <para>
  742. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  743. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  744. /// </para>
  745. /// <para>
  746. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  747. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  748. /// </para>
  749. /// <para>
  750. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  751. /// making the Subscribe or Dispose call, respectively.
  752. /// </para>
  753. /// <para>
  754. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  755. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  756. /// more concise and easier to understand.
  757. /// </para>
  758. /// </remarks>
  759. /// <seealso cref="Observable.ToEventPattern"/>
  760. #if !NO_EVENTARGS_CONSTRAINT
  761. public static IObservable<EventPattern<EventArgs>> FromEventPattern(Type type, string eventName)
  762. {
  763. if (type == null)
  764. throw new ArgumentNullException("type");
  765. if (eventName == null)
  766. throw new ArgumentNullException("eventName");
  767. return s_impl.FromEventPattern(type, eventName);
  768. }
  769. #else
  770. public static IObservable<EventPattern<object>> FromEventPattern(Type type, string eventName)
  771. {
  772. if (type == null)
  773. throw new ArgumentNullException(nameof(type));
  774. if (eventName == null)
  775. throw new ArgumentNullException(nameof(eventName));
  776. return s_impl.FromEventPattern(type, eventName);
  777. }
  778. #endif
  779. /// <summary>
  780. /// Converts a static .NET event, conforming to the standard .NET event pattern with an <see cref="EventArgs"/> parameter, to an observable sequence.
  781. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  782. /// Reflection is used to discover the event based on the specified type and the specified event name.
  783. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  784. /// </summary>
  785. /// <param name="type">Type that exposes the static event to convert.</param>
  786. /// <param name="eventName">Name of the event to convert.</param>
  787. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  788. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  789. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  790. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern.</exception>
  791. /// <remarks>
  792. /// <para>
  793. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  794. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  795. /// </para>
  796. /// <para>
  797. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  798. /// accessed from the same context, as required by some UI frameworks.
  799. /// </para>
  800. /// <para>
  801. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  802. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  803. /// parameter. For more information, see the remarks section on those overloads.
  804. /// </para>
  805. /// </remarks>
  806. /// <seealso cref="Observable.ToEventPattern"/>
  807. #if !NO_EVENTARGS_CONSTRAINT
  808. public static IObservable<EventPattern<EventArgs>> FromEventPattern(Type type, string eventName, IScheduler scheduler)
  809. {
  810. if (type == null)
  811. throw new ArgumentNullException("type");
  812. if (eventName == null)
  813. throw new ArgumentNullException("eventName");
  814. if (scheduler == null)
  815. throw new ArgumentNullException("scheduler");
  816. return s_impl.FromEventPattern(type, eventName, scheduler);
  817. }
  818. #else
  819. public static IObservable<EventPattern<object>> FromEventPattern(Type type, string eventName, IScheduler scheduler)
  820. {
  821. if (type == null)
  822. throw new ArgumentNullException(nameof(type));
  823. if (eventName == null)
  824. throw new ArgumentNullException(nameof(eventName));
  825. if (scheduler == null)
  826. throw new ArgumentNullException(nameof(scheduler));
  827. return s_impl.FromEventPattern(type, eventName, scheduler);
  828. }
  829. #endif
  830. /// <summary>
  831. /// Converts a static .NET event, conforming to the standard .NET event pattern with strongly typed event arguments, to an observable sequence.
  832. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  833. /// Reflection is used to discover the event based on the specified type and the specified event name.
  834. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  835. /// </summary>
  836. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  837. /// <param name="type">Type that exposes the static event to convert.</param>
  838. /// <param name="eventName">Name of the event to convert.</param>
  839. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  840. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> is null.</exception>
  841. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  842. /// <remarks>
  843. /// <para>
  844. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  845. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  846. /// </para>
  847. /// <para>
  848. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  849. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  850. /// </para>
  851. /// <para>
  852. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  853. /// making the Subscribe or Dispose call, respectively.
  854. /// </para>
  855. /// <para>
  856. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  857. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  858. /// more concise and easier to understand.
  859. /// </para>
  860. /// </remarks>
  861. /// <seealso cref="Observable.ToEventPattern"/>
  862. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Type type, string eventName)
  863. #if !NO_EVENTARGS_CONSTRAINT
  864. where TEventArgs : EventArgs
  865. #endif
  866. {
  867. if (type == null)
  868. throw new ArgumentNullException(nameof(type));
  869. if (eventName == null)
  870. throw new ArgumentNullException(nameof(eventName));
  871. return s_impl.FromEventPattern<TEventArgs>(type, eventName);
  872. }
  873. /// <summary>
  874. /// Converts a static .NET event, conforming to the standard .NET event pattern with strongly typed event arguments, to an observable sequence.
  875. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  876. /// Reflection is used to discover the event based on the specified type and the specified event name.
  877. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  878. /// </summary>
  879. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  880. /// <param name="type">Type that exposes the static event to convert.</param>
  881. /// <param name="eventName">Name of the event to convert.</param>
  882. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  883. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  884. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  885. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  886. /// <remarks>
  887. /// <para>
  888. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  889. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  890. /// </para>
  891. /// <para>
  892. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  893. /// accessed from the same context, as required by some UI frameworks.
  894. /// </para>
  895. /// <para>
  896. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  897. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  898. /// parameter. For more information, see the remarks section on those overloads.
  899. /// </para>
  900. /// </remarks>
  901. /// <seealso cref="Observable.ToEventPattern"/>
  902. public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Type type, string eventName, IScheduler scheduler)
  903. #if !NO_EVENTARGS_CONSTRAINT
  904. where TEventArgs : EventArgs
  905. #endif
  906. {
  907. if (type == null)
  908. throw new ArgumentNullException(nameof(type));
  909. if (eventName == null)
  910. throw new ArgumentNullException(nameof(eventName));
  911. if (scheduler == null)
  912. throw new ArgumentNullException(nameof(scheduler));
  913. return s_impl.FromEventPattern<TEventArgs>(type, eventName, scheduler);
  914. }
  915. /// <summary>
  916. /// Converts a static .NET event, conforming to the standard .NET event pattern with a strongly typed sender and strongly typed event arguments, to an observable sequence.
  917. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  918. /// Reflection is used to discover the event based on the specified type and the specified event name.
  919. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  920. /// </summary>
  921. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  922. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  923. /// <param name="type">Type that exposes the static event to convert.</param>
  924. /// <param name="eventName">Name of the event to convert.</param>
  925. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  926. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> is null.</exception>
  927. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's first argument type is not assignable to TSender. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  928. /// <remarks>
  929. /// <para>
  930. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  931. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  932. /// </para>
  933. /// <para>
  934. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEventPattern, and is used to post add and remove handler invocations.
  935. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  936. /// </para>
  937. /// <para>
  938. /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread
  939. /// making the Subscribe or Dispose call, respectively.
  940. /// </para>
  941. /// <para>
  942. /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  943. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  944. /// more concise and easier to understand.
  945. /// </para>
  946. /// </remarks>
  947. /// <seealso cref="Observable.ToEventPattern"/>
  948. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TSender, TEventArgs>(Type type, string eventName)
  949. #if !NO_EVENTARGS_CONSTRAINT
  950. where TEventArgs : EventArgs
  951. #endif
  952. {
  953. if (type == null)
  954. throw new ArgumentNullException(nameof(type));
  955. if (eventName == null)
  956. throw new ArgumentNullException(nameof(eventName));
  957. return s_impl.FromEventPattern<TSender, TEventArgs>(type, eventName);
  958. }
  959. /// <summary>
  960. /// Converts a static .NET event, conforming to the standard .NET event pattern with a strongly typed sender and strongly typed event arguments, to an observable sequence.
  961. /// Each event invocation is surfaced through an OnNext message in the resulting sequence.
  962. /// Reflection is used to discover the event based on the specified type and the specified event name.
  963. /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
  964. /// </summary>
  965. /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
  966. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  967. /// <param name="type">Type that exposes the static event to convert.</param>
  968. /// <param name="eventName">Name of the event to convert.</param>
  969. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  970. /// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
  971. /// <exception cref="ArgumentNullException"><paramref name="type"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
  972. /// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern. -or- The event's first argument type is not assignable to TSender. -or- The event's second argument type is not assignable to TEventArgs.</exception>
  973. /// <remarks>
  974. /// <para>
  975. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  976. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  977. /// </para>
  978. /// <para>
  979. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  980. /// accessed from the same context, as required by some UI frameworks.
  981. /// </para>
  982. /// <para>
  983. /// It's recommended to lift FromEventPattern calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  984. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEventPattern that omit the IScheduler
  985. /// parameter. For more information, see the remarks section on those overloads.
  986. /// </para>
  987. /// </remarks>
  988. /// <seealso cref="Observable.ToEventPattern"/>
  989. public static IObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TSender, TEventArgs>(Type type, string eventName, IScheduler scheduler)
  990. #if !NO_EVENTARGS_CONSTRAINT
  991. where TEventArgs : EventArgs
  992. #endif
  993. {
  994. if (type == null)
  995. throw new ArgumentNullException(nameof(type));
  996. if (eventName == null)
  997. throw new ArgumentNullException(nameof(eventName));
  998. if (scheduler == null)
  999. throw new ArgumentNullException(nameof(scheduler));
  1000. return s_impl.FromEventPattern<TSender, TEventArgs>(type, eventName, scheduler);
  1001. }
  1002. #endregion
  1003. #endregion
  1004. #endregion
  1005. #region + FromEvent +
  1006. #region Action<TDelegate>
  1007. /// <summary>
  1008. /// Converts a .NET event to an observable sequence, using a conversion function to obtain the event delegate. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1009. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1010. /// </summary>
  1011. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  1012. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1013. /// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying .NET event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
  1014. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1015. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1016. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1017. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  1018. /// <remarks>
  1019. /// <para>
  1020. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1021. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1022. /// </para>
  1023. /// <para>
  1024. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEvent, and is used to post add and remove handler invocations.
  1025. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  1026. /// </para>
  1027. /// <para>
  1028. /// If no SynchronizationContext is present at the point of calling FromEvent, add and remove handler invocations are made synchronously on the thread
  1029. /// making the Subscribe or Dispose call, respectively.
  1030. /// </para>
  1031. /// <para>
  1032. /// It's recommended to lift FromEvent calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  1033. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  1034. /// more concise and easier to understand.
  1035. /// </para>
  1036. /// </remarks>
  1037. /// <seealso cref="Observable.ToEvent"/>
  1038. public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(Func<Action<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  1039. {
  1040. if (conversion == null)
  1041. throw new ArgumentNullException(nameof(conversion));
  1042. if (addHandler == null)
  1043. throw new ArgumentNullException(nameof(addHandler));
  1044. if (removeHandler == null)
  1045. throw new ArgumentNullException(nameof(removeHandler));
  1046. return s_impl.FromEvent<TDelegate, TEventArgs>(conversion, addHandler, removeHandler);
  1047. }
  1048. /// <summary>
  1049. /// Converts a .NET event to an observable sequence, using a conversion function to obtain the event delegate. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1050. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1051. /// </summary>
  1052. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  1053. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1054. /// <param name="conversion">A function used to convert the given event handler to a delegate compatible with the underlying .NET event. The resulting delegate is used in calls to the addHandler and removeHandler action parameters.</param>
  1055. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1056. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1057. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  1058. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1059. /// <exception cref="ArgumentNullException"><paramref name="conversion"/> or <paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  1060. /// <remarks>
  1061. /// <para>
  1062. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1063. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1064. /// </para>
  1065. /// <para>
  1066. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  1067. /// accessed from the same context, as required by some UI frameworks.
  1068. /// </para>
  1069. /// <para>
  1070. /// It's recommended to lift FromEvent calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  1071. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEvent that omit the IScheduler
  1072. /// parameter. For more information, see the remarks section on those overloads.
  1073. /// </para>
  1074. /// </remarks>
  1075. /// <seealso cref="Observable.ToEvent"/>
  1076. public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(Func<Action<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler, IScheduler scheduler)
  1077. {
  1078. if (conversion == null)
  1079. throw new ArgumentNullException(nameof(conversion));
  1080. if (addHandler == null)
  1081. throw new ArgumentNullException(nameof(addHandler));
  1082. if (removeHandler == null)
  1083. throw new ArgumentNullException(nameof(removeHandler));
  1084. if (scheduler == null)
  1085. throw new ArgumentNullException(nameof(scheduler));
  1086. return s_impl.FromEvent<TDelegate, TEventArgs>(conversion, addHandler, removeHandler, scheduler);
  1087. }
  1088. /// <summary>
  1089. /// Converts a .NET event to an observable sequence, using a supplied event delegate type. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1090. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1091. /// </summary>
  1092. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  1093. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1094. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1095. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1096. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1097. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  1098. /// <remarks>
  1099. /// <para>
  1100. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1101. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1102. /// </para>
  1103. /// <para>
  1104. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEvent, and is used to post add and remove handler invocations.
  1105. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  1106. /// </para>
  1107. /// <para>
  1108. /// If no SynchronizationContext is present at the point of calling FromEvent, add and remove handler invocations are made synchronously on the thread
  1109. /// making the Subscribe or Dispose call, respectively.
  1110. /// </para>
  1111. /// <para>
  1112. /// It's recommended to lift FromEvent calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  1113. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  1114. /// more concise and easier to understand.
  1115. /// </para>
  1116. /// </remarks>
  1117. /// <seealso cref="Observable.ToEvent"/>
  1118. public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
  1119. {
  1120. if (addHandler == null)
  1121. throw new ArgumentNullException(nameof(addHandler));
  1122. if (removeHandler == null)
  1123. throw new ArgumentNullException(nameof(removeHandler));
  1124. return s_impl.FromEvent<TDelegate, TEventArgs>(addHandler, removeHandler);
  1125. }
  1126. /// <summary>
  1127. /// Converts a .NET event to an observable sequence, using a supplied event delegate type. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1128. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1129. /// </summary>
  1130. /// <typeparam name="TDelegate">The delegate type of the event to be converted.</typeparam>
  1131. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1132. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1133. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1134. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  1135. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1136. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  1137. /// <remarks>
  1138. /// <para>
  1139. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1140. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1141. /// </para>
  1142. /// <para>
  1143. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  1144. /// accessed from the same context, as required by some UI frameworks.
  1145. /// </para>
  1146. /// <para>
  1147. /// It's recommended to lift FromEvent calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  1148. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEvent that omit the IScheduler
  1149. /// parameter. For more information, see the remarks section on those overloads.
  1150. /// </para>
  1151. /// </remarks>
  1152. /// <seealso cref="Observable.ToEvent"/>
  1153. public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(Action<TDelegate> addHandler, Action<TDelegate> removeHandler, IScheduler scheduler)
  1154. {
  1155. if (addHandler == null)
  1156. throw new ArgumentNullException(nameof(addHandler));
  1157. if (removeHandler == null)
  1158. throw new ArgumentNullException(nameof(removeHandler));
  1159. if (scheduler == null)
  1160. throw new ArgumentNullException(nameof(scheduler));
  1161. return s_impl.FromEvent<TDelegate, TEventArgs>(addHandler, removeHandler, scheduler);
  1162. }
  1163. #endregion
  1164. #region Action<Action<TEventArgs>>
  1165. /// <summary>
  1166. /// Converts a generic Action-based .NET event to an observable sequence. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1167. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1168. /// </summary>
  1169. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1170. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1171. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1172. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1173. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  1174. /// <remarks>
  1175. /// <para>
  1176. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1177. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1178. /// </para>
  1179. /// <para>
  1180. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEvent, and is used to post add and remove handler invocations.
  1181. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  1182. /// </para>
  1183. /// <para>
  1184. /// If no SynchronizationContext is present at the point of calling FromEvent, add and remove handler invocations are made synchronously on the thread
  1185. /// making the Subscribe or Dispose call, respectively.
  1186. /// </para>
  1187. /// <para>
  1188. /// It's recommended to lift FromEvent calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  1189. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  1190. /// more concise and easier to understand.
  1191. /// </para>
  1192. /// </remarks>
  1193. /// <seealso cref="Observable.ToEvent"/>
  1194. public static IObservable<TEventArgs> FromEvent<TEventArgs>(Action<Action<TEventArgs>> addHandler, Action<Action<TEventArgs>> removeHandler)
  1195. {
  1196. if (addHandler == null)
  1197. throw new ArgumentNullException(nameof(addHandler));
  1198. if (removeHandler == null)
  1199. throw new ArgumentNullException(nameof(removeHandler));
  1200. return s_impl.FromEvent<TEventArgs>(addHandler, removeHandler);
  1201. }
  1202. /// <summary>
  1203. /// Converts a generic Action-based .NET event to an observable sequence. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1204. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1205. /// </summary>
  1206. /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
  1207. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1208. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1209. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  1210. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1211. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  1212. /// <remarks>
  1213. /// <para>
  1214. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1215. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1216. /// </para>
  1217. /// <para>
  1218. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  1219. /// accessed from the same context, as required by some UI frameworks.
  1220. /// </para>
  1221. /// <para>
  1222. /// It's recommended to lift FromEvent calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  1223. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEvent that omit the IScheduler
  1224. /// parameter. For more information, see the remarks section on those overloads.
  1225. /// </para>
  1226. /// </remarks>
  1227. /// <seealso cref="Observable.ToEvent"/>
  1228. public static IObservable<TEventArgs> FromEvent<TEventArgs>(Action<Action<TEventArgs>> addHandler, Action<Action<TEventArgs>> removeHandler, IScheduler scheduler)
  1229. {
  1230. if (addHandler == null)
  1231. throw new ArgumentNullException(nameof(addHandler));
  1232. if (removeHandler == null)
  1233. throw new ArgumentNullException(nameof(removeHandler));
  1234. if (scheduler == null)
  1235. throw new ArgumentNullException(nameof(scheduler));
  1236. return s_impl.FromEvent<TEventArgs>(addHandler, removeHandler, scheduler);
  1237. }
  1238. #endregion
  1239. #region Action<Action>
  1240. /// <summary>
  1241. /// Converts an Action-based .NET event to an observable sequence. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1242. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1243. /// </summary>
  1244. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1245. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1246. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1247. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> is null.</exception>
  1248. /// <remarks>
  1249. /// <para>
  1250. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1251. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1252. /// </para>
  1253. /// <para>
  1254. /// The current <see cref="SynchronizationContext"/> is captured during the call to FromEvent, and is used to post add and remove handler invocations.
  1255. /// This behavior ensures add and remove handler operations for thread-affine events are accessed from the same context, as required by some UI frameworks.
  1256. /// </para>
  1257. /// <para>
  1258. /// If no SynchronizationContext is present at the point of calling FromEvent, add and remove handler invocations are made synchronously on the thread
  1259. /// making the Subscribe or Dispose call, respectively.
  1260. /// </para>
  1261. /// <para>
  1262. /// It's recommended to lift FromEvent calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
  1263. /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
  1264. /// more concise and easier to understand.
  1265. /// </para>
  1266. /// </remarks>
  1267. /// <seealso cref="Observable.ToEvent"/>
  1268. public static IObservable<Unit> FromEvent(Action<Action> addHandler, Action<Action> removeHandler)
  1269. {
  1270. if (addHandler == null)
  1271. throw new ArgumentNullException(nameof(addHandler));
  1272. if (removeHandler == null)
  1273. throw new ArgumentNullException(nameof(removeHandler));
  1274. return s_impl.FromEvent(addHandler, removeHandler);
  1275. }
  1276. /// <summary>
  1277. /// Converts an Action-based .NET event to an observable sequence. Each event invocation is surfaced through an OnNext message in the resulting sequence.
  1278. /// For conversion of events conforming to the standard .NET event pattern, use any of the FromEventPattern overloads instead.
  1279. /// </summary>
  1280. /// <param name="addHandler">Action that attaches the given event handler to the underlying .NET event.</param>
  1281. /// <param name="removeHandler">Action that detaches the given event handler from the underlying .NET event.</param>
  1282. /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
  1283. /// <returns>The observable sequence that contains the event argument objects passed to the invocations of the underlying .NET event.</returns>
  1284. /// <exception cref="ArgumentNullException"><paramref name="addHandler"/> or <paramref name="removeHandler"/> or <paramref name="scheduler"/> is null.</exception>
  1285. /// <remarks>
  1286. /// <para>
  1287. /// Add and remove handler invocations are made whenever the number of observers grows beyond zero.
  1288. /// As such, an event handler may be shared by multiple simultaneously active observers, using a subject for multicasting.
  1289. /// </para>
  1290. /// <para>
  1291. /// Add and remove handler invocations are run on the specified scheduler. This behavior allows add and remove handler operations for thread-affine events to be
  1292. /// accessed from the same context, as required by some UI frameworks.
  1293. /// </para>
  1294. /// <para>
  1295. /// It's recommended to lift FromEvent calls outside event stream query expressions. This best practice reduces clutter of bridging code inside queries,
  1296. /// making the query expressions more concise and easier to understand. This has additional benefits for overloads of FromEvent that omit the IScheduler
  1297. /// parameter. For more information, see the remarks section on those overloads.
  1298. /// </para>
  1299. /// </remarks>
  1300. /// <seealso cref="Observable.ToEvent"/>
  1301. public static IObservable<Unit> FromEvent(Action<Action> addHandler, Action<Action> removeHandler, IScheduler scheduler)
  1302. {
  1303. if (addHandler == null)
  1304. throw new ArgumentNullException(nameof(addHandler));
  1305. if (removeHandler == null)
  1306. throw new ArgumentNullException(nameof(removeHandler));
  1307. if (scheduler == null)
  1308. throw new ArgumentNullException(nameof(scheduler));
  1309. return s_impl.FromEvent(addHandler, removeHandler, scheduler);
  1310. }
  1311. #endregion
  1312. #endregion
  1313. }
  1314. }