using System; using System.Collections.Generic; namespace ezxamairn { public sealed class ServiceLocator { static readonly Lazy instance = new Lazy(() => new ServiceLocator()); readonly Dictionary> registeredServices = new Dictionary>(); public static ServiceLocator Instance => instance.Value; public void Register() where TService : new() { registeredServices[typeof(TContract)] = new Lazy(() => Activator.CreateInstance(typeof(TService))); } public T Get() where T : class { Lazy service; if (registeredServices.TryGetValue(typeof(T), out service)) { return (T)service.Value; } return null; } } }