| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Extensibility.Services |
| 4 | { |
| 5 | using System; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Service provider extensions. |
| 9 | /// </summary> |
| 10 | public static class ServiceProviderExtensions |
| 11 | { |
| 12 | /// <summary> |
| 13 | /// Gets a service from the service provider. |
| 14 | /// </summary> |
| 15 | /// <typeparam name="T">Type of service to get.</typeparam> |
| 16 | /// <param name="provider">Service provider.</param> |
| 17 | public static T GetService<T>(this IServiceProvider provider) where T : class |
| 18 | { |
| 19 | return provider.GetService(typeof(T)) as T; |
| 20 | } |
| 21 | |
| 22 | /// <summary> |
| 23 | /// Gets a service from the service provider. |
| 24 | /// </summary> |
| 25 | /// <param name="provider">Service provider.</param> |
| 26 | /// <param name="serviceType">Type of service to get.</param> |
| 27 | /// <param name="service">Retrieved service.</param> |
| 28 | /// <returns>True if the service was found, otherwise false</returns> |
| 29 | public static bool TryGetService(this IServiceProvider provider, Type serviceType, out object service) |
| 30 | { |
| 31 | service = provider.GetService(serviceType); |
| 32 | return service != null; |
| 33 | } |
| 34 | |
| 35 | /// <summary> |
| 36 | /// Gets a service from the service provider. |
| 37 | /// </summary> |
| 38 | /// <typeparam name="T">Type of service to get.</typeparam> |
| 39 | /// <param name="provider">Service provider.</param> |
| 40 | /// <param name="service">Retrieved service.</param> |
| 41 | /// <returns>True if the service was found, otherwise false</returns> |
| 42 | public static bool TryGetService<T>(this IServiceProvider provider, out T service) where T : class |
| 43 | { |
| 44 | service = provider.GetService(typeof(T)) as T; |
| 45 | return service != null; |
| 46 | } |
| 47 | } |
| 48 | } |