title: How to serialize/deserialize an object loaded from another assembly?
body: <p>I want to serialize/deserialize an object that has been instantiated by another object loaded from an assembly:</p>
  
  <p>Interfaces.cs (from a referenced assembly, Interfaces.dll)</p>
  
  <pre><code>public interface ISomeInterface
  {
   ISettings Settings { get; set; }
  }
  
  public interface ISettings : ISerializable
  {
   DateTime StartDate { get; }
  }
  </code></pre>
  
  <p>SomeClass.cs (from a referenced assembly, SomeClass.dll)</p>
  
  <pre><code>public class SomeClass : ISomeInterface
  {
   private MySettings settings = new Settings();
  
   public ISettings Settings
   {
    get { return (ISettings)settings; }
    set { settings = value as MySettings; }
   }
  }
  
  [Serializable]
  public class MySettings : ISettings
  {
   private DateTime dt;
  
   public MySettings() { dt = DateTime.Now; }
  
   protected MySettings(SerializationInfo info, StreamingContext context)
   {
    dt = info.GetDateTime("dt");
   }
  
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
   public void GetObjectData(SerializationInfo info, StreamingContext context)
   {
    info.AddValue("dt", dt);
   }
  
   public DateTime StartDate
   {
    get { return startFrom; }
    internal set { startFrom = value; }
   }
  }
  </code></pre>
  
  <p>Startup project:</p>
  
  <pre><code>[Serializable]
  public class ProgramState
  }
   public ISettings Settings { get; set; }
  }
  </code></pre>
  
  <p>In the startup project, eventually I set Settings of an instance of ProgramState to Settings of SomeClass. I then go on to do the serialization using:</p>
  
  <pre><code>public void SerializeState(string filename, ProgramState ps)
  {
   Stream s = File.Open(filename, FileMode.Create);
   BinaryFormatter bFormatter = new BinaryFormatter();
   bFormatter.Serialize(s, ps);
   s.Close();
  }
  </code></pre>
  
  <p>This doesn't throw any exceptions. I deserialize with:</p>
  
  <pre><code>public ProgramState DeserializeState(string filename)
  {
   if (File.Exists(filename))
   {
    ProgramState res = new ProgramState();
    Stream s = File.Open(filename, FileMode.Open);
    BinaryFormatter bFormatter = new BinaryFormatter();
    try
    {
     res = (ProgramState)bFormatter.Deserialize(s);
    }
    catch (SerializationException se)
    {
     Debug.WriteLine(se.Message);
    }
    s.Close();
    return res;
   }
   else return new ProgramState();
  }
  </code></pre>
  
  <p>This throws an exception and the following appears in my Debug output:</p>
  
  <p>A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll<br>
  Unable to find assembly 'SomeClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.</p>
  
  <p>I'm sure that the assembly containing SomeClass has been loaded before the call to DeserializeState, so why is it throwing an exception that it is unable to find it?  </p>
  
  <p>I've been looking at some tutorials, but the ones I was able to find only deal with classes from the same assembly (plus, my understanding of the serialization and deserialization process in .NET is minimal - a link to a detailed explanation might be helpful).</p>
  
  <p>In the meantime, is there any way to make this correctly deserialize the MySettings object?</p>