ExceptionServices.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.ComponentModel;
  3. using System.Reactive.PlatformServices;
  4. namespace System.Reactive
  5. {
  6. internal static class ExceptionHelpers
  7. {
  8. private static Lazy<IExceptionServices> s_services = new Lazy<IExceptionServices>(Initialize);
  9. public static void Throw(this Exception exception)
  10. {
  11. s_services.Value.Rethrow(exception);
  12. }
  13. public static void ThrowIfNotNull(this Exception exception)
  14. {
  15. if (exception != null)
  16. s_services.Value.Rethrow(exception);
  17. }
  18. private static IExceptionServices Initialize()
  19. {
  20. return PlatformEnlightenmentProvider.Current.GetService<IExceptionServices>() ?? new DefaultExceptionServices();
  21. }
  22. }
  23. }
  24. namespace System.Reactive.PlatformServices
  25. {
  26. /// <summary>
  27. /// (Infrastructure) Services to rethrow exceptions.
  28. /// </summary>
  29. /// <remarks>
  30. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  31. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  32. /// </remarks>
  33. [EditorBrowsable(EditorBrowsableState.Never)]
  34. public interface IExceptionServices
  35. {
  36. /// <summary>
  37. /// Rethrows the specified exception.
  38. /// </summary>
  39. /// <param name="exception">Exception to rethrow.</param>
  40. void Rethrow(Exception exception);
  41. }
  42. }