namespace Samples.DI { class DISample { public static void Sample() { DIContainer container = new DIContainer(); container.Register(); Client client = new Client(container.Resolve()); client.Run(); } } public interface IService { void DoSomething(); } public class Service : IService { public void DoSomething() { Console.WriteLine("Service is doing something..."); } } public class Client { private readonly IService _service; public Client(IService service) { _service = service; } public void Run() { _service.DoSomething(); } } public class DIContainer { private readonly Dictionary _typemappings = new Dictionary(); public void Register() { _typemappings[typeof(TInterface)] = typeof(TImplementation); } public TInterface Resolve() { Type implementationType = _typemappings[typeof(TInterface)]; return (TInterface)Activator.CreateInstance(implementationType); } } }