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