title: Cast to generic type in C#
body: <p>I have a Dictionary to map a certain type to a certain generic object for that type. For example:</p>
  
  <pre><code>typeof(LoginMessage) maps to MessageProcessor&lt;LoginMessage&gt;
  </code></pre>
  
  <p>Now the problem is to retrieve this generic object at runtime from the Dictionary. Or to be more specific: To cast the retrieved object to the specific generic type.</p>
  
  <p>I need it to work something like this:</p>
  
  <pre><code>Type key = message.GetType();
  MessageProcessor&lt;key&gt; processor = messageProcessors[key] as MessageProcessor&lt;key&gt;;
  </code></pre>
  
  <p>Hope there is a easy solution to this.</p>
  
  <p>Edit:
  I do not want to use Ifs and switches. Due to performance issues I cannot use reflection of some sort either.</p>