PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms 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//===================================================================================
17using System;
18using Microsoft.Practices.ServiceLocation;
19
20namespace Microsoft.Practices.Prism
21{
22    /// <summary>
23    /// Defines extension methods for the <see cref="ServiceLocator"/> class.
24    /// </summary>
25    public static class ServiceLocatorExtensions
26    {
27        /// <summary>
28        /// Attempts to resolve specified type from the underlying <see cref="IServiceLocator"/>.
29        /// </summary>
30        /// <remarks>
31        /// This will return null on any <see cref="ActivationException"/>.</remarks>
32        /// <param name="locator">Locator to use in resolving.</param>
33        /// <param name="type">Type to resolve.</param>
34        /// <returns>T or null</returns>
35        /// <exception cref="ArgumentNullException">Thrown when <paramref name="locator"/> is <see langword="null"/>.</exception>
36        public static object TryResolve(this IServiceLocator locator, Type type)
37        {
38            if (locator == null) throw new ArgumentNullException("locator");
39
40            try
41            {
42                return locator.GetInstance(type);
43            }
44            catch (ActivationException)
45            {
46                return null;
47            }
48        }
49
50        /// <summary>
51        /// Attempts to resolve specified type from the underlying <see cref="IServiceLocator"/>.
52        /// </summary>
53        /// <remarks>
54        /// This will return null on any <see cref="ActivationException"/>.</remarks>
55        /// <typeparam name="T">Type to resolve.</typeparam>
56        /// <param name="locator">Locator to use in resolving.</param>
57        /// <returns>T or null</returns>
58        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
59        public static T TryResolve<T>(this IServiceLocator locator) where T : class
60        {
61            return locator.TryResolve(typeof(T)) as T;
62        }
63    }
64}