ExceptionServices.cs 1.7 KB

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