/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Services { public interface IService { } public interface IServiceProvider<T> { T GetService<T>(); object GetService(Type serviceType); } public sealed class ServiceProvider : IServiceProvider<IService> { private Dictionary<Type, object> _services; private ServiceProvider() { } public void RegisterService(Type type, object service) { if (!_services.ContainsKey(type)) { _services.Add(type, service); } } public T GetService<T>() where T : IService { } public object GetService(Type serviceType) { return (_services.ContainsKey(serviceType)) ? _services[serviceType] : null; } } } }