// WingsEmu // // Developed by NosWings Team using System; using System.Collections.Generic; namespace PhoenixLib.Events { /// /// Extension methods for getting services from an . /// internal static class ServiceProviderServiceExtensions { /// /// Get service of type from the . /// /// The to retrieve the service object from. /// An object that specifies the type of service object to get. /// A service object of type . /// There is no service of type . internal static object GetRequiredService(this IServiceProvider provider, Type serviceType) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (serviceType == null) { throw new ArgumentNullException(nameof(serviceType)); } object service = provider.GetService(serviceType); if (service == null) { throw new InvalidOperationException(nameof(serviceType)); } return service; } /// /// Get service of type from the . /// /// The type of service object to get. /// The to retrieve the service object from. /// A service object of type . /// There is no service of type . internal static T GetRequiredService(this IServiceProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } return (T)provider.GetRequiredService(typeof(T)); } /// /// Get an enumeration of services of type from the . /// /// The type of service object to get. /// The to retrieve the services from. /// An enumeration of services of type . internal static IEnumerable GetServices(this IServiceProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } return provider.GetRequiredService>(); } } }