This is an interesting and tricky problem. The generics definitely add to that complexity since going non-generic would be a simple IEnumerable resolution.
But... I think I can help.
You'll take advantage of...
The OnRegistered event in RegisterAssemblyTypes so you can look at what was actually registered.
The OnActivating event for the bus so you can do the registration of the handlers.
Closures to carry the list of registered handler types into the OnActivating event.
Some fancy-schmancy reflection to create the closed generic version(s) of the RegisterHandler method on the bus.
Here's a full, working example showing how to do it. Note I had to change the ICommandBus interface for RegisterHandler a bit since it wouldn't compile for me in the originally listed form, but you should be able to adapt as needed. I ran this in ScriptCs to verify.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Autofac;
public interface ICommand { }
public class CommandOne : ICommand { }
public class CommandTwo : ICommand { }
public interface ICommandHandler where T : ICommand
{
void Handle(T arg);
}
public class CommandOneHandler : ICommandHandler
{
public void Handle(CommandOne arg) { }
}
public class CommandTwoHandler : ICommandHandler
{
public void Handle(CommandTwo arg) { }
}
public interface ICommandBus
{
IEnumerable