Reputation: 34800
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?
}
}
}
}
Upvotes: 3
Views: 3256
Reputation: 5128
What if inside your Queue
method you'd queue not events but rather the code which emits them?
private ConcurrentQueue<Action> handlers = new ConcurrentQueue<Action>();
public void Queue<TEvent>(TEvent e)
{
handlers.Enqueue(new Action(() =>
{
foreach (var handler in GetHandlers<TEvent>())
{
handler(e);
}
}));
}
public void Emit()
{
Action act;
while (handlers.TryDequeue(out act))
{
act();
}
}
private IEnumerable<Action<TEvent>> GetHandlers<TEvent>()
{
return ObjectFactory.GetAllInstances(typeof(Action<TEvent>>));
}
Hope this helps.
Upvotes: 1
Reputation: 1500515
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
.
Upvotes: 6