Covariance and Contravariance for Action Delegates

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

    The type parameter of Action is contravariant: you can assign an Action to an Action because obviously a method that can work on any object can also work on a string.

    What you do here is trying to bill a method that works on a CancelCommand (derived type) as a method that works on any IDomainCommand (base type). This is logically wrong, so the compiler doesn't let you do it -- if it did, you could just invoke upperAction passing it a DifferentTypeOfDomainCommand.

    提交回复
    热议问题