From 4e8d3e32c4110275bde7b8b8c6dc9a838c4107d1 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 5 Oct 2017 14:55:55 -0400 Subject: [PATCH] Adding methods for removing services --- ICD.Common.Utils/Services/ServiceProvider.cs | 61 +++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) 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 } }