PlatformEnlightenmentProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 HAS_TPL46
  66. if (t == typeof(ITaskServices))
  67. {
  68. return (T)(object)new TaskServicesImpl();
  69. }
  70. #endif
  71. if (t == typeof(IQueryServices))
  72. {
  73. //
  74. // We perform this Debugger.IsAttached check early rather than deferring
  75. // the decision to intercept query operator methods to the debugger
  76. // assembly that's dynamically discovered here. Also, it's a reasonable
  77. // expectation it'd be pretty hard to turn on interception dynamically
  78. // upon a debugger attach event, so we should make this check early.
  79. //
  80. // In the initial release of v2.0 (RTM), we won't have the corresponding
  81. // debugger assembly available yet, so the dynamic load would always
  82. // fail. We also don't want to take the price of (an attempt to) a dynamic
  83. // assembly load for the regular production case.
  84. //
  85. if (Debugger.IsAttached)
  86. {
  87. #if NETCF35
  88. var name = "System.Reactive.Linq.QueryDebugger, System.Reactive.Debugger";
  89. #else
  90. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  91. var ifType = t.GetTypeInfo();
  92. #else
  93. var ifType = t;
  94. #endif
  95. var asm = new AssemblyName(ifType.Assembly.FullName);
  96. asm.Name = "System.Reactive.Debugger";
  97. var name = "System.Reactive.Linq.QueryDebugger, " + asm.FullName;
  98. #endif
  99. var dbg = Type.GetType(name, false);
  100. if (dbg != null)
  101. return (T)(object)Activator.CreateInstance(dbg);
  102. }
  103. }
  104. return null;
  105. }
  106. }
  107. }