title: Dictionary of Action<T> Delegates
body: <p>I have object <strong>XML serialized messages</strong> coming into a class called <strong>MessageRouter</strong>. The XML contains the Type name it it was serialized from, and I need to be able to <strong>invoke different delegate methods depending on the type that are not known until runtime</strong>. I'm not extremely strong in generics so hopefully this will make sense to someone...</p>
  
  <p>I'd like MessageRouter to provide a <strong>RegisterDelegateForType</strong> method like so:</p>
  
  <pre><code>myMessageRouter.RegisterDelegateForType(new Action&lt;MySerializableType&gt;(myActionHandler));
  </code></pre>
  
  <p>And then store the types, or the type's string representation in a Dictionary like this:</p>
  
  <pre><code>Dictionary&lt;Type, Action&lt;T&gt;&gt; registeredDelegates;
  </code></pre>
  
  <p>That way, I can do something like the following pseudocode, calling the type's assigned delegate and passing the deserialized object:</p>
  
  <pre><code>Type xmlSerializedType = TypeFromXmlString(incomingXml);
  object deserializedObject = DeserializeObjectFromXml(xmlSerializedType, incomingXml);
  
  // then invoke the action and pass in the deserialized object
  registeredDelegates[xmlSerializedType](deserializedObject);
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>How do you define a Dictionary that can contain a <code>Type</code> as a key and a generic <code>Action&lt;T&gt;</code> as a value, and have the RegisterDelegateForType method populate the dictionary?</li>
  <li>If that's not possible, what's the best way to do this?</li>
  </ol>