Co or Contravariance vor Action Delegates
<p>I'm stuck, why isn't this working:</p>

<pre><code>  var childAction = new Action&lt;CancelCommand&gt;(blabla);
  Action&lt;IDomainCommand&gt; upperAction = (Action&lt;IDomainCommand&gt;) childAction;
</code></pre>

<p>Why isn't this working if CancelCommand implements IDomainCommand?
By the way: Is this co or contravariance what i'm trying to do here? ;)</p>

<p>thank you in advance
just the best
laurin</p>

<p><strong>EDIT</strong>
Thank you guys for your very fast responses!</p>

<p>The problem why i need this is because i construct a generic Action Template.
I have the following Interface:</p>

<pre><code>IMessageBus.RegisterAction&lt;T&gt;(Action&lt;T&gt; registerAction) where T : IDomainCommand
</code></pre>

<p>Because i have to build this action on runtime my code lookes as the follwing:</p>

<pre><code>var genericExecuteAction = this.GetType().GetMethod("ExecuteCommandAction",
                                                            BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(commandType);

var actionType = typeof(Action&lt;&gt;).MakeGenericType(commandType);
var @delegate = Delegate.CreateDelegate(actionType, this, genericExecuteAction);
var actionDelegate = (Action&lt;DomainCommandBase&gt;)@delegate;

messageBus.Register(actionDelegate);
</code></pre>

<p>The problem is i need to cast it so that i can pass it to this method.
You see?
Behind the mesasge bus we are using is using RX and unfortunately all the methods there use generics, there a now non-generic-overloads.</p>
