title: Create instance using ctor injection and ServiceProvider
body: <p>I know there is <code>IServiceCollection</code> interface where I can register my services and <code>IServiceProvider</code> which can instantiate the services.</p>
  
  <p>How do I instantiate a class, based on specified Type, which uses registered services?</p>
  
  <pre><code>class MyClass
  {
      public MyClass(ISomeService someService) { }
  }
  
  var serviceCollection = new ServiceCollection();
  serviceCollection.AddSingleton&lt;ISomeService, SomeService&gt;()
  
  MyClass instance = CreateInstance(typeof(MyClass));
  
  object CreateIntance(Type type)
  {
     ???
  }
  </code></pre>
  
  <p>For example, how does ASP.NET Core creates controller instances?</p>
  
  <p>I've made naive implementation of the activator but isn't there something like this in .NET Core already?</p>
  
  <pre><code>private static object CreateInstance(Type type, IServiceProvider serviceProvider)
  {
      var ctor = type.GetConstructors()
          .Where(c =&gt; c.IsPublic)
          .OrderByDescending(c =&gt; c.GetParameters().Length)
          .FirstOrDefault()
          ?? throw new InvalidOperationException($"No suitable contructor found on type '{type}'");
  
      var injectionServices = ctor.GetParameters()
          .Select(p =&gt; serviceProvider.GetRequiredService(p.ParameterType))
          .ToArray();
  
      return ctor.Invoke(injectionServices);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>EDIT: this is my scenario. I've refactored some legacy code that implements this interface.</p>
  
  <pre><code>public interface IEventDispatcher
  {
      void RegisterHandler(Type handler);
      void Dispatch(DomainEvent @event);
  }
  </code></pre>
  
  <p>In the Dispatch method implementation I create instances of the handlers:</p>
  
  <pre><code>public class InMemoryBus : IEventDispatcher
  {
      private readonly List&lt;Type&gt; _handlers = new List&lt;Type&gt;();
      private readonly Func&lt;Type, object&gt; activator;
  
      /// &lt;param name="activator"&gt;Used to create instance of message handlers&lt;/param&gt;
      public InMemoryBus(Func&lt;Type, object&gt; activator)
      {
          this.activator = activator;
      }
  
      public void Dispatch(DomainEvent @event)
      {
          Type messageType = message.GetType();
          var openInterface = typeof(IHandleMessages&lt;&gt;);
          var closedInterface = openInterface.MakeGenericType(messageType);
  
          var handlersToNotify = from h in _handlers
                                 where closedInterface.GetTypeInfo().IsAssignableFrom(h.GetTypeInfo())
                                 select h;
          foreach (Type h in _handlersToNotify)
          {
              //this is the tricky part
              var handlerInstance = activator(h);
              closedInterface.GetTypeInfo()
                  .GetMethod(nameof(IHandleMessages&lt;T&gt;.Handle))
                  .Invoke(handlerInstance, new[] { message });
          }
      }
  
      public void RegisterHandler(Type type) =&gt; _handlers.Add(type);
  }
  </code></pre>