PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism/ServiceLocatorExtensions.cs

#
C# | 64 lines | 25 code | 3 blank | 36 comment | 2 complexity | aa87949dfe098dcd6de92f37b82416ae MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using Microsoft.Practices.ServiceLocation;
  19. namespace Microsoft.Practices.Prism
  20. {
  21. /// <summary>
  22. /// Defines extension methods for the <see cref="ServiceLocator"/> class.
  23. /// </summary>
  24. public static class ServiceLocatorExtensions
  25. {
  26. /// <summary>
  27. /// Attempts to resolve specified type from the underlying <see cref="IServiceLocator"/>.
  28. /// </summary>
  29. /// <remarks>
  30. /// This will return null on any <see cref="ActivationException"/>.</remarks>
  31. /// <param name="locator">Locator to use in resolving.</param>
  32. /// <param name="type">Type to resolve.</param>
  33. /// <returns>T or null</returns>
  34. /// <exception cref="ArgumentNullException">Thrown when <paramref name="locator"/> is <see langword="null"/>.</exception>
  35. public static object TryResolve(this IServiceLocator locator, Type type)
  36. {
  37. if (locator == null) throw new ArgumentNullException("locator");
  38. try
  39. {
  40. return locator.GetInstance(type);
  41. }
  42. catch (ActivationException)
  43. {
  44. return null;
  45. }
  46. }
  47. /// <summary>
  48. /// Attempts to resolve specified type from the underlying <see cref="IServiceLocator"/>.
  49. /// </summary>
  50. /// <remarks>
  51. /// This will return null on any <see cref="ActivationException"/>.</remarks>
  52. /// <typeparam name="T">Type to resolve.</typeparam>
  53. /// <param name="locator">Locator to use in resolving.</param>
  54. /// <returns>T or null</returns>
  55. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
  56. public static T TryResolve<T>(this IServiceLocator locator) where T : class
  57. {
  58. return locator.TryResolve(typeof(T)) as T;
  59. }
  60. }
  61. }