22

I am serializing an structure by using BinaryFormatter using this code:

private void SerializeObject(string filename, SerializableStructure objectToSerialize)
{
    Stream stream = File.Open(filename, FileMode.Create);
    BinaryFormatter bFormatter = new BinaryFormatter();
    bFormatter.Serialize(stream, objectToSerialize);
    stream.Close();
}

Which objectToSerialize is my structure, I'm calling this function like this:

SerializableStructure s = new SerializableStructure();
s.NN = NN;
s.SubNNs = SubNNs;
s.inputs = inputs;
SerializeObject(Application.StartupPath + "\\Save\\" + txtSave.Text + ".bin", s);

Which SerializableStructure, and Type of NN, SubNNs and inputs are serializable. (inputs contains some Points, Rectangles and generic lists).

Now, When I run my code, I am given this error:

Type 'MainProject.Main' in Assembly 'MainProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Why I'm given this error? Main is my form, and these variables are located in my form.

I have successfully serialized Type of NN with MemoryStream and VB.NET , But I don't know why I'm getting this error?

Here is the definition of my structures:

SerializableStructure:

[Serializable()]
public class SerializableStructure
{
    public List<Inputs> inputs = new List<Inputs>();
    public NeuralNetwork NN;
    public NeuralNetwork[] SubNNs;
}

Inputs:

[Serializable()]
public class Inputs
{
    public string XPath { get; set; }
    public string YPath { get; set; }
    public string ImagePath { get; set; }
    public string CharName { get; set; }
    public string CharBaseName { get; set; }
    public List<double> x { get; set; }
    public List<double> y { get; set; }
    public List<double> DotsX { get; set; }
    public List<double> DotsY { get; set; }
    public List<Point> GravityCenters { get; set; }
    public List<Rectangle> Bounds { get; set; }

    public override string ToString()
    {
        return CharName;
    }

    public Inputs(string xPath, string yPath, string imagePath, string charName, string charBaseName)
    {
        XPath = xPath;
        YPath = yPath;
        ImagePath = imagePath;
        CharName = charName;
        CharBaseName = charBaseName;
        x = new List<double>();
        y = new List<double>();
        GravityCenters = new List<Point>();
        Bounds = new List<Rectangle>();
    }
}

Also NN is very big structure(!).

6
  • Could you provide the definition of the structure you are serializing? I'm interested in types of SubNNs, NN, and inputs. Dec 24, 2011 at 11:11
  • Does NeuralNetwork have delegates? Dec 24, 2011 at 11:16
  • @dasblinkenlight No, NeuralNetwork hasn't any delegates. Dec 24, 2011 at 18:53
  • Could you post NN's data members somewhere? If it does not fit in the question, you can post it to the pastebin.com. Dec 24, 2011 at 19:01
  • @dasblinkenlight I'll post it into that site for about 1-2 hours later. Dec 24, 2011 at 19:03

2 Answers 2

20

This almost alwas means you have an event (or other delegate - maybe a callback) somewhere in your object model, that is trying to be serialized. Add [NonSerialized] to any event-backing fields. If you are using a field-like event (the most likely kind), this is:

[field:NonSerialized]
public event SomeDelegateType SomeEventName;

Alternatively: most other serializers don't look at events/delegates, and provide better version-compatibility. Switching to XmlSerializer, JavaScriptSerializer, DataContractSerializer or protobuf-net (just 4 examples) would also solve this by the simple approach of not trying to do this (you almost never intend for events to be considered as part of a DTO).

8
  • my NN class in VB.NET language, how can I set the events to NonSerialized in VB.NET? Dec 24, 2011 at 19:02
  • @Mahdi like this: stackoverflow.com/questions/4377806/… Dec 24, 2011 at 19:23
  • System.Net.Http.HttpRequestMessage not serializable. Assembly : System.Net.Http, Version=4.0.0.0
    – Kiquenet
    Mar 13, 2018 at 11:20
  • @Kiquenet ok... so... don't serialize that? Mar 13, 2018 at 11:36
  • 1
    @Kiquenet yeah, that's just not reliable and makes it very hard to unpack later if the code reading the exception data doesn't have all the same assemblies (and the exact same version of the assemblies). You're much better off doing something involving dumping the .Message, .StackTrace etc for each as simple strings. Crude? yes; but: reliable Mar 16, 2018 at 10:39
5

The problem is that you are trying to serialize a class derived from Form. The Form class is fundamentally unserializable. It has an enormous amount of internal state that is highly runtime dependent. That starts with an obvious property like Handle, a value that's always different. Less obvious are properties like Size, dependent on user preferences like the size of the font for the window caption. Ends with all the text, location and sizes for the controls, they are subject to localization. The odds that a serialized Form object can be properly deserialized anywhere at any time to create an exact clone of the form are zero.

Microsoft made no bones about it when they wrote the code, they simply omitted the [Serializable] attribute from the class declaration. Which is why you get the exception.

You'll have to aim lower, write your own class to capture your form's state. And give it the attribute. You'll need to write a bunch of code that maps between the form and control properties to an object of that class, back and forth.

5
  • 1
    My interpretation of the question is that the form being included was an unexpected surprise Dec 24, 2011 at 22:44
  • Fair call, wish we could see the definition of SerializableStructure. Wish the OP realized it matters :) Dec 24, 2011 at 22:51
  • @Marc - I'm curious about one thing, how did you know that somebody else posted an answer to this question? Accident, tool or relentlessly keeping track? Dec 24, 2011 at 23:08
  • Random luck - this page was still on my browser, and I wanted to check for inbox, so hit refresh. Dec 24, 2011 at 23:19
  • I wish it wasn't random. Must have been mulled over at meta before :( Dec 24, 2011 at 23:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.