PlatformEnlightenmentProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 readonly 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. public static IPlatformEnlightenmentProvider Current
  43. {
  44. get
  45. {
  46. return s_current;
  47. }
  48. }
  49. private static IPlatformEnlightenmentProvider CreatePlatformProvider()
  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. return (IPlatformEnlightenmentProvider)Activator.CreateInstance(t);
  70. else
  71. return new DefaultPlatformEnlightenmentProvider();
  72. }
  73. }
  74. class DefaultPlatformEnlightenmentProvider : IPlatformEnlightenmentProvider
  75. {
  76. public T GetService<T>(object[] args) where T : class
  77. {
  78. return null;
  79. }
  80. }
  81. }