I've created a simple message bus that queues and emits/publishes events.
I'm using StructureMap to locate the registered handlers (Action<T>) of the event but am not sure how to cast it from the object that StructureMap returns into an invoke-able action.
Since I can't cast to Action<object> I'm assuming that Action<T> is not covariant? Can this be done another way?
public class Bus
{
private ConcurrentQueue<object> events = new ConcurrentQueue<object>();
public void Queue<TEvent>(TEvent e)
{
events.Enqueue(e);
}
public void Emit()
{
object e;
while (events.TryDequeue(out e))
{
var handlerType = typeof(Action<>).MakeGenericType(e.GetType());
foreach (var handler in ObjectFactory.GetAllInstances(handlerType))
{
// how to invoke action?
}
}
}
}