ExceptionServices.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. return PlatformEnlightenmentProvider.Current.GetService<IExceptionServices>() ?? new DefaultExceptionServices();
  23. }
  24. }
  25. }
  26. namespace System.Reactive.PlatformServices
  27. {
  28. /// <summary>
  29. /// (Infrastructure) Services to rethrow exceptions.
  30. /// </summary>
  31. /// <remarks>
  32. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  33. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  34. /// </remarks>
  35. [EditorBrowsable(EditorBrowsableState.Never)]
  36. public interface IExceptionServices
  37. {
  38. /// <summary>
  39. /// Rethrows the specified exception.
  40. /// </summary>
  41. /// <param name="exception">Exception to rethrow.</param>
  42. void Rethrow(Exception exception);
  43. }
  44. }