title: The given key was not present in the dictionary on Activator.CreateInstance
body: <p>I hope you can give me a hand on this. It's been bothering me for a quite while already. and I find no conclusive answers on the web. I have a simple class hierarchy: BaseClass with ClassA, ClassB and DefaultClass derived from it. They all have a constructor like ClassA(Input input) : base(input) ... all very simple. Now I have the following factory</p>
  
  <pre><code>namespace mynamespace
  {
      public class Factory
      {
  
          public static Dictionary&lt;string, Type&gt; Map = 
          new Dictionary&lt;string, Type&gt;
          {
              {"ClassA", typeof(ClassA)},
              {"ClassB", typeof(ClassB)}
          };
          public static BaseClass Create(Input input)
          {
              Type constructor;
              try
              {
                  constructor = Map[input.ClassType];
              }
              catch (KeyNotFoundException)
              {
                  constructor = typeof(DefaultClass);
              }
              return (BaseClass) Activator.CreateInstance(constructor, input);
          }
      }
  }
  </code></pre>
  
  <p>As you see, the .ClassType member of the input object determines what class is instantiated. This type of construct has been working for a simple call like</p>
  
  <pre><code>var myClass = Factory.Create(input);
  </code></pre>
  
  <p>in my standalone application. Now I must have the application running on a server and the call to the Create method receives a serialized input object from the client. I would say that's the only difference between the application that works and the one that doesn't. Now, input is something along the lines</p>
  
  <pre><code>[DataContract]
  public class Input
  {
      [DataMember] public string ClassType;
      [DataMember] public string member1;
  }
  </code></pre>
  
  <p>wheras before it had no such DataContract or DataMember annotations. Well, now the the code crashed at the Activator.CreateInstance line with an exception "the given key was not present in dictionary". Do you have any clue of why this could be? The only difference is the serialized object in the constructor.</p>
  
  <p>Thanks a lot in advance and regards.</p>