存储在列表中时无法使用 Autofac 解决命令

Commands can not be solved with Autofac when stored in a List

如果有下面的代码,实现命令模式。我想将几个命令存储在一个列表中,然后从列表中选择它们,解析一个 commandhandler,最后执行命令。

在实施这个问题时我 运行 解决了一个问题,解决单个命令从 Autofac 工作但解决存储在列表中的命令引发异常告诉我即使是同一个命令也找不到命令处理程序因为我之前解决了 commandhandler。

    public static void ShowResolveProblem()
    {
        var action = new DisplayMessageAction("Hello");
        var actionhandler = GetActionHandler(action); // this works well

        var actions = new List<IAction>();
        actions.Add(action);
        actionhandler = GetActionHandler(actions[0]); // this throws exception
    }

这就是解析方法

    private static IActionHandler<T> GetActionHandler<T>(T action) where T : IAction
    {
        var container = GetActionHandlerContainer();

        return container.Resolve<IActionHandler<T>>();
    }

有谁知道如何得到这个运行?

如果您在解析 actionHandler 时没有具体类型,您可以使用 typeof(IActionHandler<>).MakeGenericType(action.GetType()) 获取它。

要在没有 T 的情况下使用 IActionHandler<T>,您必须创建一个新的 IActionHandler 界面:

class Program
{
    static void Main(string[] args)
    {
        ContainerBuilder builder = new Autofac.ContainerBuilder();
        builder.RegisterAssemblyTypes(typeof(IActionHandler<>).Assembly)
               .AsClosedTypesOf(typeof(IActionHandler<>));

        IContainer container = builder.Build();

        List<IAction> actions = new List<IAction>();
        actions.Add(new DisplayMessageAction("Test1"));
        actions.Add(new DisplayMessageAction("Test2"));
        actions.Add(new BeepMessageAction(200, 200));

        foreach (IAction action in actions)
        {
            Type actionHandlerType = typeof(IActionHandler<>).MakeGenericType(action.GetType()); 
            IActionHandler actionHandler = (IActionHandler)container.Resolve(actionHandlerType);
            actionHandler.Execute(action);
        }
    }
}

public interface IAction { }
public interface IActionHandler
{
    void Execute(IAction action);
}
public interface IActionHandler<T> : IActionHandler
    where T : IAction
{
    void Execute(T IAction);
}

public abstract class ActionHandlerBase<T> : IActionHandler<T>
    where T : IAction
{
    void IActionHandler.Execute(IAction action)
    {
        this.Execute((T)action);
    }

    public abstract void Execute(T IAction);
}

public class DisplayMessageAction : IAction
{
    public DisplayMessageAction(String message)
    {
        this._message = message;
    }

    private readonly String _message;

    public String Message
    {
        get
        {
            return this._message;
        }
    }
}
public class DisplayMessageActionHandler : ActionHandlerBase<DisplayMessageAction>
{
    public override void Execute(DisplayMessageAction action)
    {
        Console.WriteLine(action.Message);
    }
}

public class BeepMessageAction : IAction
{
    public BeepMessageAction(Int32 frequency, Int32 duration)
    {
        this._frequency = frequency;
        this._duration = duration;
    }

    private readonly Int32 _frequency;
    private readonly Int32 _duration;

    public Int32 Frequency
    {
        get
        {
            return this._frequency;
        }
    }
    public Int32 Duration
    {
        get
        {
            return this._duration;
        }
    }
}
public class BeepMessageActionHandler : ActionHandlerBase<BeepMessageAction>
{
    public override void Execute(BeepMessageAction action)
    {
        Console.Beep(action.Frequency, action.Duration);
    }
}