title: System.InvalidCastException: Unable to cast object of type x to type y
body: <p>I have a custom messaging system which I am trying to convert so it can support plugins.</p>
  
  <p>The system has producers and consumers.
  Both must specify the type of message they produce or consume.</p>
  
  <p>For consumers the Interface to be implemented is </p>
  
  <pre><code>IConsumer&lt;AMessage&gt;
  </code></pre>
  
  <p>I have a consumer in a plugin dll which implements this Interface:</p>
  
  <pre><code>FileProcessor : IConsumer&lt;FileMessage&gt;
  </code></pre>
  
  <p>With </p>
  
  <pre><code>FileMessage : AMessage
  </code></pre>
  
  <p>And AMessage is an abstract class.
  Both IConsumer and AMessage are in the main (Core) assembly.</p>
  
  <p>Now, in the core, which loaded the plugin and scanned &amp; found the consumers I want to link the consumers into the messaging system.</p>
  
  <p>To simplify things here I just try to put the consumer in a variable:</p>
  
  <pre><code>IConsumer&lt;AMessage&gt; consumer = IConsumer&lt;AMessage&gt; new FileProcessor();
  </code></pre>
  
  <p>I get a warning in VS:</p>
  
  <blockquote>
    <p>Suspicious cast:there is no type in the solution which is inherited
    from both 'TestPlugin.FileProcessor' and 'Core.IConsumer'.</p>
  </blockquote>
  
  <p>And when I execute I get </p>
  
  <blockquote>
    <p>System.InvalidCastException: Unable to cast object of type
    'TestPlugin.FileProcessor' to type 'Core.IConsumer`1[Core.AMessage]'.</p>
  </blockquote>
  
  <p>Why does this cast fail? How can I solve this?</p>
  
  <p><strong>--edit1--</strong></p>
  
  <p>based on comments, here is the full definition of IProducer and IConsumer:</p>
  
  <pre><code>public interface IProducer&lt;out T&gt; : IProcessor where T : AMessage
  {
      event MessageSender&lt;T&gt; SendMessage;
  }
  
  public interface IConsumer&lt;in T&gt; : IProcessor where T : AMessage
  {
      ProcessResult ProcessBean(T bean);
  }
  </code></pre>
  
  <p>when Producers are linked in the system, they define T, after that only consumers with the same type of T can be linked to that producer.
  For example:</p>
  
  <pre><code>var channel = flow.From(FileProducer).To(FileConsumer);
  </code></pre>
  
  <p>With flow.From:</p>
  
  <pre><code>public Channel&lt;T&gt; From&lt;T&gt;(IProducer&lt;T&gt; producer) where T : AMessage
  {
      ...
  }
  </code></pre>
  
  <p>and channel.To:</p>
  
  <pre><code>public class Channel&lt;T&gt; where T : AMessage 
  {
  
     public Channel&lt;T&gt; To(IConsumer&lt;T&gt; consumer){
        ...
     }
  }
  </code></pre>
  
  <p>So the producer defines the type of T for the consumers.</p>
  
  <p><strong>--edit1--</strong></p>
  
  <p><strong>--edit2--</strong></p>
  
  <p>The need for the down-cast is because I am trying to reconstruct the channels from an JSON definition in the Core system.</p>
  
  <p>So Consumers (and Producers) are dynamically constructed from the plugins assemblies and added to a temporary dictionary (when detected and instantiated):</p>
  
  <pre><code>var consumers = new Dictionary&lt;string, IProcessor&gt;();
  </code></pre>
  
  <p>I cannot use IConsumer here for the consumers since T is not necessarily the same for all consumers.</p>
  
  <p>Then when building the Channels, I'll lookup the consumer by it's id (key of dictionary) and provide it to the To(...) method of the channel. That fails because the dictionary holds IProcessor and the dynamically constructed channel has a "To" signature of  <code>public Channel&lt;T&gt; To(IConsumer&lt;T&gt; consumer)</code>.</p>
  
  <p><strong>--edit2--</strong></p>
  
  <p><strong>--solution--</strong></p>
  
  <p>Thanks to Luaan (see comments) I reached to the following solution:</p>
  
  <p>When scanning the plugin assemblies I originally stored the detected Producers and Consumers in a dictionary of <code>&lt;string, IProcessor&gt;</code>, now I changed this dictionary to <code>&lt;string, dynamic&gt;</code> which solved this casting issue!</p>
  
  <p>Thanks to all who added to getting to this solution!</p>
  
  <p>@Luaan, I wished I could select your answer as the solution, but it is in a comment...</p>
  
  <p><strong>--solution--</strong></p>
  
  <p>Thanks!</p>