开发者

InvalidProgramException when calling Execute on DelegateCommand sub-class

I have a sub-class of DelegateCommand which fails with an InvalidProgramException, "The Command Language Runtime detected an invalid program." when I call Execute.

My sub-cla开发者_C百科ss looks like this:

    public class SaveOrderCommand : DelegateCommand<IOrder>
{
    [Dependency]
    public IOrderService OrderService { get; set; }

    public SaveOrderCommand()
        : base(p => this.ExecuteSaveOrder(p), p => this.CanExecuteSaveOrder(p))
    { }

    public bool CanExecuteSaveOrder(IOrder order)
    {
        return true;
    }

    public void ExecuteSaveOrder(IOrder order)
    {
        if (order == null)
            return;

        IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
        order = productSpecificOrderService.Save(order);
    }
}

I can see that giving the base class references to methods in the sub-class might be an issue but if that's the issue, can anyone suggest a better implementation?


Perhaps you should just implement ICommand yourself? Your class would become:

public class SaveOrderCommand : ICommand
{
    [Dependency]
    public IOrderService OrderService { get; set; }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return parameter is IOrder;
    }

    public void Execute(object parameter)
    {
        var order = parameter as IOrder;
        if (order == null)
            return;

        IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
        order = productSpecificOrderService.Save(order);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜