How to cast object to Action<T>

六月ゝ 毕业季﹏ 提交于 2021-02-07 20:31:40

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/20800075/how-to-cast-object-to-actiont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!