PlatformEnlightenmentProvider.cs 4.2 KB

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