PlatformEnlightenmentProvider.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Reflection;
  6. namespace System.Reactive.PlatformServices
  7. {
  8. /// <summary>
  9. /// (Infrastructure) Interface for enlightenment providers.
  10. /// </summary>
  11. /// <remarks>
  12. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  13. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  14. /// </remarks>
  15. [EditorBrowsable(EditorBrowsableState.Never)]
  16. public interface IPlatformEnlightenmentProvider
  17. {
  18. /// <summary>
  19. /// (Infastructure) Tries to gets the specified service.
  20. /// </summary>
  21. /// <typeparam name="T">Service type.</typeparam>
  22. /// <param name="args">Optional set of arguments.</param>
  23. /// <returns>Service instance or null if not found.</returns>
  24. T GetService<T>(params object[] args) where T : class;
  25. }
  26. /// <summary>
  27. /// (Infrastructure) Provider for platform-specific framework enlightenments.
  28. /// </summary>
  29. /// <remarks>
  30. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  31. /// </remarks>
  32. [EditorBrowsable(EditorBrowsableState.Never)]
  33. public static class PlatformEnlightenmentProvider
  34. {
  35. private static IPlatformEnlightenmentProvider s_current = CreatePlatformProvider();
  36. /// <summary>
  37. /// (Infrastructure) Gets the current enlightenment provider. If none is loaded yet, accessing this property triggers provider resolution.
  38. /// </summary>
  39. /// <remarks>
  40. /// This member is used by the Rx infrastructure and not meant for public consumption or implementation.
  41. /// </remarks>
  42. [Obsolete("This mechanism will be removed in the next major version", false)]
  43. public static IPlatformEnlightenmentProvider Current
  44. {
  45. get
  46. {
  47. return s_current;
  48. }
  49. set
  50. {
  51. if (value == null) throw new ArgumentNullException(nameof(value));
  52. s_current = value;
  53. }
  54. }
  55. private static IPlatformEnlightenmentProvider CreatePlatformProvider()
  56. {
  57. //
  58. // TODO: Investigate whether we can simplify this logic to just use "System.Reactive.PlatformServices.PlatformEnlightenmentProvider, System.Reactive.PlatformServices".
  59. // It turns out this doesn't quite work on Silverlight. On the other hand, in .NET Compact Framework 3.5, we mysteriously have to use that path.
  60. //
  61. #if NETCF35
  62. var name = "System.Reactive.PlatformServices.CurrentPlatformEnlightenmentProvider, System.Reactive.PlatformServices";
  63. #else
  64. #if CRIPPLED_REFLECTION && HAS_WINRT
  65. var ifType = typeof(IPlatformEnlightenmentProvider).GetTypeInfo();
  66. #else
  67. var ifType = typeof(IPlatformEnlightenmentProvider);
  68. #endif
  69. var asm = new AssemblyName(ifType.Assembly.FullName);
  70. asm.Name = "System.Reactive.PlatformServices";
  71. var name = "System.Reactive.PlatformServices.CurrentPlatformEnlightenmentProvider, " + asm.FullName;
  72. #endif
  73. var t = Type.GetType(name, false);
  74. if (t != null)
  75. return (IPlatformEnlightenmentProvider)Activator.CreateInstance(t);
  76. else
  77. return new DefaultPlatformEnlightenmentProvider();
  78. }
  79. }
  80. class DefaultPlatformEnlightenmentProvider : IPlatformEnlightenmentProvider
  81. {
  82. public T GetService<T>(object[] args) where T : class
  83. {
  84. return null;
  85. }
  86. }
  87. }