1
0

CurrentPlatformEnlightenmentProvider.cs 3.8 KB

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