4 using System.Collections.Generic;
7 namespace SiliconStudio.Presentation.ViewModel
14 private readonly List<object> services =
new List<object>();
15 private bool DisallowRegister {
get; set; }
26 if (DisallowRegister)
throw new InvalidOperationException(
"Unable to register a service in this service provider.");
27 if (service == null)
throw new ArgumentNullException(
"service");
28 if (services.Any(x => x.GetType() == service.GetType()))
29 throw new InvalidOperationException(
"A service of the same type has already been registered.");
31 services.Add(service);
38 provider.services.AddRange(services);
43 public object TryGet(Type serviceType)
45 if (serviceType == null)
throw new ArgumentNullException(
"serviceType");
46 object serviceFound = null;
48 foreach (var service
in services.Where(serviceType.IsInstanceOfType))
50 if (serviceFound != null)
51 throw new InvalidOperationException(
"Multiple services match the given type.");
53 serviceFound = service;
59 public T TryGet<T>() where T :
class
61 return (T)TryGet(typeof(T));
65 public object Get(Type serviceType)
67 var result = TryGet(serviceType);
68 if (result == null)
throw new InvalidOperationException(
"No service matches the given type.");
73 public T Get<T>() where T :
class
75 var result = (T)TryGet(typeof(T));
76 if (result == null)
throw new InvalidOperationException(
"No service matches the given type.");
object TryGet(Type serviceType)
Gets a service of the given type, if available. The type of service to retrieve.An instance of the se...
A service provider class for view model objects.
A service provider class for view model objects.
object Get(Type serviceType)
Gets a service of the given type. The type of service to retrieve.An instance of the service that mat...
IViewModelServiceProvider Clone()
Clones this IViewModelServiceProvider using shallow copy.
void RegisterService(object service)
Register a new service in this ViewModelServiceProvider.