Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:41:41+00:00 2026-05-18T08:41:41+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:41:42+00:00Added an answer on May 18, 2026 at 8:41 am

    I did a stupid mistake. Instead of this:

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

    I was writing this:

    info.AddValue("id", typeof(int));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why do I get following error when trying to start a ruby on rails
I get the following error when trying to run the latest Cygwin version of
I'm trying to call the SQL statement below but get the following error: System.Data.SqlClient.SqlException:
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I am trying to insert a time only value, but get the following error
I am trying to import a site collection and I get the following error:
I get the following error when trying to push changes to github from the
Hi currently I'm trying to get following snippet of code to work: function Entry()
I get the following error, when trying to save a properties file (containing name/value
I am trying to use delayed_job on heroku and I get the following error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.