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