PlatformEnlightenmentProvider.cs 4.0 KB

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