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?
}
}
}
}

Since I can't cast to Action I'm assuming that Action is not covariant?
Action<T> is contravariant - which makes sense, because an Action<object> can be treated as an Action<string> (both can accept a string reference) but an Action<string> can't be treated as an Action<object> (what would you expect it to do if you provided it a non-string reference?).
The simplest way to invoke this is probably to just use Delegate.DynamicInvoke to be honest - another alternative would be to write a generic method and invoke it either with reflection or using dynamic.
See more on this question at Stackoverflow