title: How to serialize & deserialize types derived from an abstract class that are unknown until runtime?
body: <p>I'm having a problem with XML serializing and deserializing a class which contains a collection of instances of a concrete classes that implement a base abstract class.</p>
  
  <p>My solution is organized like so:</p>
  
  <ol>
  <li>A class library project containing an abstract class called Base.</li>
  <li>Another class library project, which is used as a plugin, containing a concrete class called Derived which implements Base. I am going to have more than one project like this, because I'm going to load them as plugins on the main project.</li>
  <li>An app, which is the main project. It knows uses the first project as a reference, and loads plugin assemblies which export a concrete class that implement Base, like the second project, on runtime. On startup, after loading the assemblies using reflection, it loads serialized instances of classes which implement Base and deserializes them on shutdown.</li>
  </ol>
  
  <p>Here's an example of my classes:</p>
  
  <p>Lets say I have this abstract class in <strong>one project</strong>, called BaseClassProj:</p>
  
  <pre><code>public abstract class Base
  {
      public int  BaseProp { get; set; }
  }
  </code></pre>
  
  <p>I have <strong>another project</strong>, DerivedClassProj, in which I implement the Base abstract class, and add some properties:</p>
  
  <pre><code>public class Derived : Base
  {
      public string DerivedProp { get; set; }
  }
  </code></pre>
  
  <p>And then there's a <strong>third and main project</strong> in which I have a class which contains a dictionary which key type is the name of the type which has to be some derived class of Base, and the value is a collection of instances of that given type. <strong>ObservableDictionary is XML serializable</strong>, implementation is <a href="http://pastebin.com/ustJcZyf" rel="nofollow">here</a>. For example:</p>
  
  <pre><code>public class Manager
  {
      public ObservableDictionary&lt;string, ObservableCollection&lt;Base&gt;&gt; Instances { get; set; }
  }
  </code></pre>
  
  <p>The problem is that during runtime I load the types to my main project, so I won't know the type I'm about to load in advance which means I <strong>cannot use XmlInclude</strong>, which is why I'm using this:</p>
  
  <pre><code>// Type = Base, extraTypes = { Derived, [another derived class, ...] }
  public XmlSerializer(Type type, Type[] extraTypes){..}
  </code></pre>
  
  <p>overload of XmlSerializer constructor in order to serialize and deserialize the main class.</p>
  
  <p>When I tried to serialize the Manager class, an InvalidOperationException was thrown:</p>
  
  <blockquote>
    <p>The type Derived (or any other concrete class that implements Base) was not expected. Use XmlInclude or SoapInclude attribute to specify types that are not known statically.</p>
  </blockquote>
  
  <p>Just like I said, I <strong>do not know</strong> the types I'm handling when serializing and deserializing in advance because I load them on runtime using reflection, therefore I am not able to use XmlInclude attribute on Base class. The only thing I know before serializing or deserializing is that Base is assignable from the types I'm dealing with.</p>
  
  <p>So, what can the problem be? I initialized XmlSerializer with typeof(Base) as the main type and an array of types of which Base is assignable.</p>
  
  <p>Thanks in advance! I've been trying to figure out solutions for my requirements and searching all over the internet all day long.</p>