Ivan
Ivan

Reputation: 177

"Exception has been thrown by the target of an invocation." when trying to deserialize an object

I get the following error when trying to deserialize an object in C#.

Exception has been thrown by the target of an invocation.

The relevant part of the 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<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute> 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<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>)info.GetValue("attributes", typeof(List<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>));
    }

    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<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>));
    }

    public Consumer Convert()
    {
        return new Consumer(requirement, dialect, initialId, id, iteration, attributes);
    }
}

[Serializable()]
class ListOfConsumers : ISerializable
{
    List<ConsumerSerialized> consumers = null;

    public List<ConsumerSerialized> Consumers
    {
        get { return consumers; }
        set { consumers = value; }
    }

    public ListOfConsumers(List<ConsumerSerialized> consumers)
    {
        this.consumers = consumers;
    }

    public ListOfConsumers(SerializationInfo info, StreamingContext ctxt)
    {
        this.consumers = (List<ConsumerSerialized>)info.GetValue("Consumers", typeof(List<ConsumerSerialized>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("Consumers", this.consumers);
    }
}

class ConsumersSerializer
{
    List<ConsumerSerialized> ConvertConsumers(List<Consumer> consumers)
    {
        List<ConsumerSerialized> result = new List<ConsumerSerialized>();
        foreach (Consumer c in consumers)
            result.Add(new ConsumerSerialized(c));

        return result;
    }

    List<Consumer> ConvertConsumers(List<ConsumerSerialized> consumers)
    {
        List<Consumer> result = new List<Consumer>();
        foreach (ConsumerSerialized c in consumers)
            result.Add(c.Convert());

        return result;
    }

    public void SerializeObject(List<Consumer> 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<Consumer> 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;
        }
    }
}

I'm doing the serialization this way:

 List<Consumer> initialConsumers = ...
 ConsumersSerializer cs = new ConsumersSerializer();
 cs.SerializeObject(initialConsumers);

And the deserialization this way:

ConsumersSerializer cs = new ConsumersSerializer();
List<Consumer> initialConsumers = cs.DeSerializeObject();

Any thoughts?

Thanks. Ivan

Upvotes: 1

Views: 13410

Answers (1)

Ivan
Ivan

Reputation: 177

I did a stupid mistake. Instead of this:

info.AddValue("id", this.id);

I was writing this:

info.AddValue("id", typeof(int));

Upvotes: 12

Related Questions