diff --git a/ICD.Common.Utils/Services/ServiceProvider.cs b/ICD.Common.Utils/Services/ServiceProvider.cs index 7bfcac0..57ab020 100644 --- a/ICD.Common.Utils/Services/ServiceProvider.cs +++ b/ICD.Common.Utils/Services/ServiceProvider.cs @@ -152,6 +152,38 @@ namespace ICD.Common.Services Instance.AddServiceInstance(tService, service); } + /// + /// Attempts to remove the given service from the given type. + /// + /// + /// + /// + [PublicAPI] + public static bool RemoveService(TService service) + { +// ReSharper disable once CompareNonConstrainedGenericWithNull + if (service == null) + throw new ArgumentNullException("service"); + + return RemoveService(typeof(TService), service); + } + + /// + /// Attempts to remove the given service from the given type. + /// + /// + /// + /// + [PublicAPI] + public static bool RemoveService(Type tService, object service) + { + // ReSharper disable once CompareNonConstrainedGenericWithNull + if (service == null) + throw new ArgumentNullException("service"); + + return Instance.RemoveServiceInstance(tService, service); + } + #endregion #region Private Methods @@ -204,7 +236,6 @@ namespace ICD.Common.Services /// /// /// - [PublicAPI] private void AddServiceInstance(Type tService, object service) { if (tService == null) @@ -231,6 +262,34 @@ namespace ICD.Common.Services } } + /// + /// Removes the given service from the given type. + /// + /// + /// + private bool RemoveServiceInstance(Type tService, object service) + { + if (tService == null) + throw new ArgumentNullException("tService"); + + if (service == null) + throw new ArgumentNullException("service"); + + try + { + m_ServicesSection.Enter(); + + if (!m_Services.ContainsKey(tService)) + return false; + + return m_Services[tService] == service && m_Services.Remove(tService); + } + finally + { + m_ServicesSection.Leave(); + } + } + #endregion } }