title: "Exception has been thrown by the target of an invocation." when trying to deserialize an object
body: <p>I get the following error when trying to deserialize an object in C#.</p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation.</p>
  </blockquote>
  
  <p>The relevant part of the code:</p>
  
  <pre><code>    [Serializable()]
      public struct SlaAttribute : ISerializable
      {
          static int nextId = 1;
  
          public int id;
          public string parameterName;
          public string parameterNameInInitialTemplate;
          public string unit;
  
          public SlaAttribute(SerializationInfo info, StreamingContext ctxt)
          {
              this.id = (int)info.GetValue("id", typeof(int));
              this.parameterName = (string)info.GetValue("parameterName", typeof(string));
              this.parameterNameInInitialTemplate = (string)info.GetValue("parameterNameInInitialTemplate", typeof(string));
              this.unit = (string)info.GetValue("unit", typeof(string));
          }
  
          public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
          {
              info.AddValue("id", typeof(int));
              info.AddValue("parameterName", typeof(string));
              info.AddValue("parameterNameInInitialTemplate", typeof(string));
              info.AddValue("unit", typeof(string));
          }
      }
  
  
  
  [Serializable()]
  class ConsumerSerialized : ISerializable
  {
      string requirement;
      string dialect;
      int initialId;
      int id;
      int iteration;
      List&lt;VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute&gt; attributes;
  
      public ConsumerSerialized(SerializationInfo info, StreamingContext ctxt)
      {
          this.requirement = (string)info.GetValue("requirement", typeof(string));
          this.dialect = (string)info.GetValue("dialect", typeof(string));
          this.initialId = (int)info.GetValue("initialId", typeof(int));
          this.id = (int)info.GetValue("id", typeof(int));
          this.iteration = (int)info.GetValue("iteration", typeof(int));
          this.attributes = (List&lt;VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute&gt;)info.GetValue("attributes", typeof(List&lt;VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute&gt;));
      }
  
      public ConsumerSerialized(Consumer consumer)
      {
          this.requirement = consumer.requirement;
          this.dialect = consumer.dialect;
          this.initialId = consumer.initialId;
          this.id = consumer.id;
          this.iteration = consumer.iteration;
          this.attributes = consumer.attributes;
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
      {
          info.AddValue("requirement", typeof(string));
          info.AddValue("dialect", typeof(string));
          info.AddValue("initialId", typeof(int));
          info.AddValue("id", typeof(int));
          info.AddValue("iteration", typeof(int));
          info.AddValue("attributes", typeof(List&lt;VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute&gt;));
      }
  
      public Consumer Convert()
      {
          return new Consumer(requirement, dialect, initialId, id, iteration, attributes);
      }
  }
  
  [Serializable()]
  class ListOfConsumers : ISerializable
  {
      List&lt;ConsumerSerialized&gt; consumers = null;
  
      public List&lt;ConsumerSerialized&gt; Consumers
      {
          get { return consumers; }
          set { consumers = value; }
      }
  
      public ListOfConsumers(List&lt;ConsumerSerialized&gt; consumers)
      {
          this.consumers = consumers;
      }
  
      public ListOfConsumers(SerializationInfo info, StreamingContext ctxt)
      {
          this.consumers = (List&lt;ConsumerSerialized&gt;)info.GetValue("Consumers", typeof(List&lt;ConsumerSerialized&gt;));
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
      {
          info.AddValue("Consumers", this.consumers);
      }
  }
  
  class ConsumersSerializer
  {
      List&lt;ConsumerSerialized&gt; ConvertConsumers(List&lt;Consumer&gt; consumers)
      {
          List&lt;ConsumerSerialized&gt; result = new List&lt;ConsumerSerialized&gt;();
          foreach (Consumer c in consumers)
              result.Add(new ConsumerSerialized(c));
  
          return result;
      }
  
      List&lt;Consumer&gt; ConvertConsumers(List&lt;ConsumerSerialized&gt; consumers)
      {
          List&lt;Consumer&gt; result = new List&lt;Consumer&gt;();
          foreach (ConsumerSerialized c in consumers)
              result.Add(c.Convert());
  
          return result;
      }
  
      public void SerializeObject(List&lt;Consumer&gt; listOfConsumers)
      {
          ListOfConsumers consumers = new ListOfConsumers(ConvertConsumers(listOfConsumers));
          Stream stream = File.Open(Settings.FilePathSerializator, FileMode.Create);
          BinaryFormatter bFormatter = new BinaryFormatter();
          bFormatter.Serialize(stream, consumers);
          stream.Close();
      }
  
      public List&lt;Consumer&gt; DeSerializeObject()
      {
          try
          {
              Stream stream = File.Open(Settings.FilePathSerializator, FileMode.Open);
              BinaryFormatter bFormatter = new BinaryFormatter();
              ListOfConsumers consumers = (ListOfConsumers)bFormatter.Deserialize(stream);
              stream.Close();
              return ConvertConsumers(consumers.Consumers);
          }
          catch (Exception ex)
          {
              System.Console.WriteLine(ex.Message);
              throw ex;
          }
      }
  }
  </code></pre>
  
  <p>I'm doing the serialization this way:</p>
  
  <pre><code> List&lt;Consumer&gt; initialConsumers = ...
   ConsumersSerializer cs = new ConsumersSerializer();
   cs.SerializeObject(initialConsumers);
  </code></pre>
  
  <p>And the deserialization this way:</p>
  
  <pre><code>ConsumersSerializer cs = new ConsumersSerializer();
  List&lt;Consumer&gt; initialConsumers = cs.DeSerializeObject();
  </code></pre>
  
  <p>Any thoughts?</p>
  
  <p>Thanks.
  Ivan</p>