@@ -41,13 +41,14 namespace Implab { | |||
|
41 | 41 | public bool TryGetService<T>(out T service) { |
|
42 | 42 | AssertNotDisposed(); |
|
43 | 43 | |
|
44 | try { | |
|
45 | service = GetService<T>(); | |
|
46 | return true; | |
|
47 | } catch(KeyNotFoundException) { | |
|
48 | service = default(T); | |
|
49 | return false; | |
|
50 | } | |
|
44 | var result = GetService(typeof(T), false); | |
|
45 | if (result == null) { | |
|
46 | service = default(T); | |
|
47 | return false; | |
|
48 | } else { | |
|
49 | service = (T)result; | |
|
50 | return true; | |
|
51 | } | |
|
51 | 52 | } |
|
52 | 53 | |
|
53 | 54 | /// <summary> |
@@ -56,7 +57,11 namespace Implab { | |||
|
56 | 57 | /// <param name="serviceType">Тип запрашиваемого сервиса</param> |
|
57 | 58 | /// <returns>Объект, реализующий сервис</returns> |
|
58 | 59 | /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception> |
|
59 |
|
|
|
60 | public object GetService(Type serviceType) { | |
|
61 | return GetService (serviceType, true); | |
|
62 | } | |
|
63 | ||
|
64 | public virtual object GetService(Type serviceType, bool throwOnError) { | |
|
60 | 65 | if (serviceType == null) |
|
61 | 66 | throw new ArgumentNullException("serviceType"); |
|
62 | 67 | AssertNotDisposed(); |
@@ -119,7 +124,9 namespace Implab { | |||
|
119 | 124 | return se.service; |
|
120 | 125 | } |
|
121 | 126 | |
|
122 | throw new Exception("Unable to create a service instance"); | |
|
127 | if (throwOnError) | |
|
128 | throw new Exception("Unable to create a service instance"); | |
|
129 | return null; | |
|
123 | 130 | } |
|
124 | 131 | |
|
125 | 132 | /// <summary> |
General Comments 0
You need to be logged in to leave comments.
Login now