Covariance and Contravariance for Action Delegates

前端 未结 4 1166
长发绾君心
长发绾君心 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:13

    Your forgetting how these delegates are being used

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

    means that upperAction will at some point in the future be called, passing some form of an IDomainCommand object. You are giving it an function which can only handle CancelCommand objects. But there are (probably) other classes which implement IDomainCommand, and upperAction could potenially be called with any of them.

提交回复
热议问题