diff --git a/Implab/ServiceLocator.cs b/Implab/ServiceLocator.cs --- a/Implab/ServiceLocator.cs +++ b/Implab/ServiceLocator.cs @@ -41,13 +41,14 @@ namespace Implab { public bool TryGetService(out T service) { AssertNotDisposed(); - try { - service = GetService(); - return true; - } catch(KeyNotFoundException) { - service = default(T); - return false; - } + var result = GetService(typeof(T), false); + if (result == null) { + service = default(T); + return false; + } else { + service = (T)result; + return true; + } } /// @@ -56,7 +57,11 @@ namespace Implab { /// Тип запрашиваемого сервиса /// Объект, реализующий сервис /// Сервис не зарегистрирован - public object GetService(Type serviceType) { + public object GetService(Type serviceType) { + return GetService (serviceType, true); + } + + public virtual object GetService(Type serviceType, bool throwOnError) { if (serviceType == null) throw new ArgumentNullException("serviceType"); AssertNotDisposed(); @@ -119,7 +124,9 @@ namespace Implab { return se.service; } - throw new Exception("Unable to create a service instance"); + if (throwOnError) + throw new Exception("Unable to create a service instance"); + return null; } ///