PlatformEnlightenmentProvider.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 IPlatformEnlightenmentProvider s_current = CreatePlatformProvider();
  34. /// <summary>
  35. /// (Infrastructure) Gets the current enlightenment provider. If none is loaded yet, accessing this property triggers provider resolution.
  36. /// </summary>
  37. /// <remarks>
  38. /// This member is used by the Rx infrastructure and not meant for public consumption or implementation.
  39. /// </remarks>
  40. public static IPlatformEnlightenmentProvider Current
  41. {
  42. get
  43. {
  44. return s_current;
  45. }
  46. }
  47. private static IPlatformEnlightenmentProvider CreatePlatformProvider()
  48. {
  49. //
  50. // TODO: Investigate whether we can simplify this logic to just use "System.Reactive.PlatformServices.PlatformEnlightenmentProvider, System.Reactive.PlatformServices".
  51. // 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.
  52. //
  53. #if NETCF35
  54. var name = "System.Reactive.PlatformServices.CurrentPlatformEnlightenmentProvider, System.Reactive.PlatformServices";
  55. #else
  56. #if CRIPPLED_REFLECTION && HAS_WINRT
  57. var ifType = typeof(IPlatformEnlightenmentProvider).GetTypeInfo();
  58. #else
  59. var ifType = typeof(IPlatformEnlightenmentProvider);
  60. #endif
  61. var asm = new AssemblyName(ifType.Assembly.FullName);
  62. asm.Name = "System.Reactive.PlatformServices";
  63. var name = "System.Reactive.PlatformServices.CurrentPlatformEnlightenmentProvider, " + asm.FullName;
  64. #endif
  65. var t = Type.GetType(name, false);
  66. if (t != null)
  67. return (IPlatformEnlightenmentProvider)Activator.CreateInstance(t);
  68. else
  69. return new DefaultPlatformEnlightenmentProvider();
  70. }
  71. }
  72. class DefaultPlatformEnlightenmentProvider : IPlatformEnlightenmentProvider
  73. {
  74. public T GetService<T>(object[] args) where T : class
  75. {
  76. return null;
  77. }
  78. }
  79. }