PlatformEnlightenmentProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. //
  3. // WARNING: The full namespace-qualified type name should stay the same for the discovery in System.Reactive.Core to work!
  4. //
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Reactive;
  8. using System.Reactive.Concurrency;
  9. using System.Reactive.Linq;
  10. using System.Reflection;
  11. namespace System.Reactive.PlatformServices
  12. {
  13. /// <summary>
  14. /// (Infrastructure) Provider for platform-specific framework enlightenments.
  15. /// </summary>
  16. [EditorBrowsable(EditorBrowsableState.Never)]
  17. public class CurrentPlatformEnlightenmentProvider : IPlatformEnlightenmentProvider
  18. {
  19. /// <summary>
  20. /// (Infastructure) Tries to gets the specified service.
  21. /// </summary>
  22. /// <typeparam name="T">Service type.</typeparam>
  23. /// <param name="args">Optional set of arguments.</param>
  24. /// <returns>Service instance or null if not found.</returns>
  25. public virtual T GetService<T>(object[] args) where T : class
  26. {
  27. var t = typeof(T);
  28. #if HAS_EDI
  29. if (t == typeof(IExceptionServices))
  30. {
  31. return (T)(object)new ExceptionServicesImpl();
  32. }
  33. #endif
  34. #if !NO_THREAD || WINDOWS
  35. if (t == typeof(IConcurrencyAbstractionLayer))
  36. {
  37. return (T)(object)new ConcurrencyAbstractionLayerImpl();
  38. }
  39. #endif
  40. if (t == typeof(IScheduler) && args != null)
  41. {
  42. switch ((string)args[0])
  43. {
  44. #if !WINDOWS && !NO_THREAD
  45. case "ThreadPool":
  46. return (T)(object)ThreadPoolScheduler.Instance;
  47. #elif WINDOWS
  48. case "ThreadPool":
  49. return (T)(object)ThreadPoolScheduler.Default;
  50. #endif
  51. #if !NO_TPL
  52. case "TaskPool":
  53. return (T)(object)TaskPoolScheduler.Default;
  54. #endif
  55. case "NewThread":
  56. return (T)(object)NewThreadScheduler.Default;
  57. }
  58. }
  59. #if WINDOWS || WINDOWSPHONE7
  60. if (t == typeof(IHostLifecycleNotifications))
  61. {
  62. return (T)(object)new HostLifecycleNotifications();
  63. }
  64. #endif
  65. if (t == typeof(IQueryServices))
  66. {
  67. //
  68. // We perform this Debugger.IsAttached check early rather than deferring
  69. // the decision to intercept query operator methods to the debugger
  70. // assembly that's dynamically discovered here. Also, it's a reasonable
  71. // expectation it'd be pretty hard to turn on interception dynamically
  72. // upon a debugger attach event, so we should make this check early.
  73. //
  74. // In the initial release of v2.0 (RTM), we won't have the corresponding
  75. // debugger assembly available yet, so the dynamic load would always
  76. // fail. We also don't want to take the price of (an attempt to) a dynamic
  77. // assembly load for the regular production case.
  78. //
  79. if (Debugger.IsAttached)
  80. {
  81. #if NETCF35
  82. var name = "System.Reactive.Linq.QueryDebugger, System.Reactive.Debugger";
  83. #else
  84. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  85. var ifType = t.GetTypeInfo();
  86. #else
  87. var ifType = t;
  88. #endif
  89. var asm = new AssemblyName(ifType.Assembly.FullName);
  90. asm.Name = "System.Reactive.Debugger";
  91. var name = "System.Reactive.Linq.QueryDebugger, " + asm.FullName;
  92. #endif
  93. var dbg = Type.GetType(name, false);
  94. if (dbg != null)
  95. return (T)(object)Activator.CreateInstance(dbg);
  96. }
  97. }
  98. return null;
  99. }
  100. }
  101. }