protected static T LocateStatLightService <T>() where T : class { T service = null; try { Assembly[] list; #if WINDOWS_PHONE if (typeof(T) == typeof(ITestRunnerHost)) { return(new StatLight.Client.Harness.Hosts.MSTest.MSTestRunnerHost() as T); } throw new NotSupportedException("type({0}) is not supported.".FormatWith(typeof(T).FullName)); #else list = Deployment.Current.Parts .Where(w => w.Source.Contains("StatLight", StringComparison.OrdinalIgnoreCase)) .Select(ap => System.Windows.Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative))) .Select(stream => new System.Windows.AssemblyPart().Load(stream.Stream)).ToArray(); var type = list .SelectMany(s => s.GetTypes()) .Where(w => w != typeof(T)) .Where(w => typeof(T).IsAssignableFrom(w)) .ToArray(); if (type.Length == 0) { throw new StatLightException( "Could not locate an instance of [{0}] in the following assemblies [{1}]".FormatWith( typeof(T).FullName, string.Join(" - " + Environment.NewLine, list.Select(s => s.FullName).ToArray()))); } if (type.Length > 1) { throw new StatLightException( "Found multiple types that could be assignable from [{0}]. The types are [{1}]. The following assemblies were scanned [{2}]".FormatWith( typeof(T).FullName, string.Join(" - " + Environment.NewLine, type.Select(s => s.FullName).ToArray()), string.Join(" - " + Environment.NewLine, list.Select(s => s.FullName).ToArray()))); } service = (T)Activator.CreateInstance(type.Single()); #endif } catch (ReflectionTypeLoadException rfex) { ReflectionInfoHelper.HandleReflectionTypeLoadException(rfex); } if (service == null) { Server.Trace("Could not locate service {0}.".FormatWith(typeof(T).FullName)); } return(service); }
protected void DisplayTestHarness() { if (TestRunConfigurationDownloadComplete && CompletedTestXapRequest) { try { var rootVisual = TestRunnerHost.StartRun(); OnReady(rootVisual); } catch (ReflectionTypeLoadException ex) { ReflectionInfoHelper.HandleReflectionTypeLoadException(ex); } } }
protected static T LocateService <T>() where T : class { T service = null; try { Assembly[] list = System.Windows.Deployment.Current.Parts.Select( ap => System.Windows.Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative))).Select( stream => new System.Windows.AssemblyPart().Load(stream.Stream)).ToArray(); var type = list .SelectMany(s => s.GetTypes()) .Where(w => w != typeof(T)) .Where(w => typeof(T).IsAssignableFrom(w)) .ToArray(); if (type.Length == 0) { throw new StatLightException( "Could not locate an instance of [{0}] in the following assemblies [{1}]".FormatWith( typeof(T).FullName, string.Join(" - " + Environment.NewLine, list.Select(s => s.FullName).ToArray()))); } if (type.Length > 1) { throw new StatLightException( "Found multiple types that could be assignable from [{0}]. The types are [{1}]. The following assemblies were scanned [{2}]".FormatWith( typeof(T).FullName, string.Join(" - " + Environment.NewLine, type.Select(s => s.FullName).ToArray()), string.Join(" - " + Environment.NewLine, list.Select(s => s.FullName).ToArray()))); } service = (T)Activator.CreateInstance(type.Single()); } catch (ReflectionTypeLoadException rfex) { ReflectionInfoHelper.HandleReflectionTypeLoadException(rfex); } if (service == null) { Server.Trace("Could not locate service {0}.".FormatWith(typeof(T).FullName)); } return(service); }