ExceptionServices.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.ComponentModel;
  5. using System.Reactive.PlatformServices;
  6. namespace System.Reactive
  7. {
  8. internal static class ExceptionHelpers
  9. {
  10. private static Lazy<IExceptionServices> s_services = new Lazy<IExceptionServices>(Initialize);
  11. public static void Throw(this Exception exception)
  12. {
  13. s_services.Value.Rethrow(exception);
  14. }
  15. public static void ThrowIfNotNull(this Exception exception)
  16. {
  17. if (exception != null)
  18. s_services.Value.Rethrow(exception);
  19. }
  20. private static IExceptionServices Initialize()
  21. {
  22. #pragma warning disable CS0618 // Type or member is obsolete
  23. return PlatformEnlightenmentProvider.Current.GetService<IExceptionServices>() ?? new DefaultExceptionServices();
  24. #pragma warning restore CS0618 // Type or member is obsolete
  25. }
  26. }
  27. }
  28. namespace System.Reactive.PlatformServices
  29. {
  30. /// <summary>
  31. /// (Infrastructure) Services to rethrow exceptions.
  32. /// </summary>
  33. /// <remarks>
  34. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  35. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  36. /// </remarks>
  37. [EditorBrowsable(EditorBrowsableState.Never)]
  38. public interface IExceptionServices
  39. {
  40. /// <summary>
  41. /// Rethrows the specified exception.
  42. /// </summary>
  43. /// <param name="exception">Exception to rethrow.</param>
  44. void Rethrow(Exception exception);
  45. }
  46. }