Covariance and Contravariance for Action Delegates

前端 未结 4 1221
长发绾君心
长发绾君心 2021-01-27 10:59

I\'m stuck, why isn\'t this working:

  var childAction = new Action(blabla);
  Action upperAction = (Action

        
4条回答
  •  忘了有多久
    2021-01-27 11:14

    Hang on, why do you want this to work?

    Your childAction is something that accepts a CancelCommand and does something with it.

    But upperAction could be invoked on any IDomainCommand, not just a CancelCommand. Supposing the above compiled; what should then happen when I do

    upperAction(new EditCommand());
    

    where class EditCommand : IDomainCommand ?

    If you attempt an assignment the other way round:

            var upperAction = new Action(idc => { });
            Action childAction = upperAction;
    

    it works and you don't even need a cast. Any Action counts as an Action.

提交回复
热议问题